

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/dzx-blog/img/fluid.png">
  <link rel="icon" href="/dzx-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="Java基础变量数据类型[]里的数字代表所占字节 基本数据类型  数值型 byte[1] short[2] int[4] long[8]   字符型  char[2] 存放单个字符’a’ 可汉字 布尔型  boolean[1] 存放true false  引用数据类型  类 class 接口 interface 数组 []  数据类型转换  低精度可以向高精度转换  例如: byte -&gt;">
<meta property="og:type" content="article">
<meta property="og:title" content="Java基础学习">
<meta property="og:url" content="https://deng-zixiong.gitee.io/dzx-blog/2022/12/06/Java%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="猛男邓子雄的博客">
<meta property="og:description" content="Java基础变量数据类型[]里的数字代表所占字节 基本数据类型  数值型 byte[1] short[2] int[4] long[8]   字符型  char[2] 存放单个字符’a’ 可汉字 布尔型  boolean[1] 存放true false  引用数据类型  类 class 接口 interface 数组 []  数据类型转换  低精度可以向高精度转换  例如: byte -&gt;">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-12-05T16:00:00.000Z">
<meta property="article:modified_time" content="2023-07-19T05:30:53.398Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>Java基础学习 - 猛男邓子雄的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/dzx-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/dzx-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/dzx-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"deng-zixiong.gitee.io","root":"/dzx-blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/dzx-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/dzx-blog/js/utils.js" ></script>
  <script  src="/dzx-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/dzx-blog/">
      <strong>邓子雄的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/dzx-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Java基础学习"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-12-06 00:00" pubdate>
          2022年12月6日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          36k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          299 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Java基础学习</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="Java基础"><a href="#Java基础" class="headerlink" title="Java基础"></a>Java基础</h1><h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><p>[]里的数字代表所占字节</p>
<p><strong>基本数据类型</strong></p>
<ul>
<li>数值型<ul>
<li>byte[1]</li>
<li>short[2]</li>
<li>int[4]</li>
<li>long[8]</li>
</ul>
</li>
<li>字符型  char[2] 存放单个字符’a’ 可汉字</li>
<li>布尔型  boolean[1] 存放true false</li>
</ul>
<p><strong>引用数据类型</strong></p>
<ul>
<li>类 class</li>
<li>接口 interface</li>
<li>数组 []</li>
</ul>
<p><strong>数据类型转换</strong></p>
<ul>
<li>低精度可以向高精度转换  例如: byte -&gt; short</li>
<li>byte short char 参与运算 都会转换成int</li>
<li>表达式结果的类型自动提升为 操作数中最大的类型</li>
<li>可以使用各个类型的包装类方法 parsexxx来转换</li>
</ul>
<h3 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h3><p><strong>注意点:</strong></p>
<ul>
<li><p>局部变量没有默认值 需要赋值才能使用.全局变量有默认值</p>
</li>
<li><p>局部变量不能加修饰符</p>
</li>
</ul>
<h2 id="文档使用"><a href="#文档使用" class="headerlink" title="文档使用"></a>文档使用</h2><h3 id="查找方法步骤"><a href="#查找方法步骤" class="headerlink" title="查找方法步骤"></a>查找方法步骤</h3><p>java包 -&gt; 类 -&gt; 方法</p>
<h2 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h2><h3 id="普通运算符"><a href="#普通运算符" class="headerlink" title="普通运算符"></a>普通运算符</h3><p><strong>注意点</strong></p>
<ol>
<li><p>运算的结果会自动选择最高精度</p>
<ol>
<li><p>&#96;&#96;&#96;java<br>&#x2F;&#x2F;因为10 和4 都是整数  结果默认保存整数<br>stystem.out.println(10&#x2F;4) &#x2F;&#x2F;输出结果为2 而不是2.5</p>
<p>double d &#x3D; 10 &#x2F;5<br>stystem.out.println(10&#x2F;4) &#x2F;&#x2F;输出结果为2.0</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs awk"><br><span class="hljs-number">2</span>. 取模 % 的本质<br><br>   <span class="hljs-number">1</span>. ```java<br>      <span class="hljs-regexp">//</span>取模符号公式<br>      <span class="hljs-regexp">//</span>a % b = a -a / b *b<br>      <span class="hljs-regexp">//</span> -<span class="hljs-number">10</span> - (-<span class="hljs-number">10</span>)/ <span class="hljs-number">3</span> * <span class="hljs-number">3</span> = -<span class="hljs-number">10</span> + <span class="hljs-number">9</span> = -<span class="hljs-number">1</span><br>      <span class="hljs-regexp">//</span>	<span class="hljs-number">10</span> /<span class="hljs-number">3</span> 取整<br>      System.out.println(-<span class="hljs-number">10</span> % <span class="hljs-number">3</span>) <span class="hljs-regexp">//</span>-<span class="hljs-number">1</span><br></code></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>自增(自减)</p>
<ol>
<li><p>i++  后++  先赋值  再运算 再比较</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span><br><span class="hljs-type">int</span> <span class="hljs-variable">k</span> <span class="hljs-operator">=</span> j++<br>Stystem.out.println(k,j) <span class="hljs-comment">// 1 2</span><br></code></pre></td></tr></table></figure>
</li>
<li><p>++i  前++ 先运算 先比较 再赋值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span><br><span class="hljs-type">int</span> <span class="hljs-variable">k</span> <span class="hljs-operator">=</span> ++j<br>Stystem.out.println(k,j) <span class="hljs-comment">// 2 2</span><br></code></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
<h3 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h3><p><strong>注意点</strong></p>
<ul>
<li><p>&amp;&amp;(短路语) 与 &amp;的区别</p>
<ul>
<li><p>&#96;&#96;&#96;java<br>int a &#x3D; 4<br>int b &#x3D; 9<br>&#x2F;&#x2F;&amp;&amp; 当前一个条件不满足时 后一个语句不执行<br>if(a &lt; 1 &amp;&amp; ++b &lt; 50) {<br>System.out.println(‘123’)<br>}<br>&#x2F;&#x2F;a &#x3D;4, b &#x3D; 9 </p>
<p>&#x2F;&#x2F;&amp; 前一个条件即使不满足 也会执行下一条语句<br>if(a &lt; 1 &amp;&amp; ++b &lt; 50) {<br>System.out.println(‘123’)<br>}<br>&#x2F;&#x2F;a &#x3D; 4 , b&#x3D; 10</p>
<figure class="highlight angelscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs angelscript"><br>- || 与 | 同理<br><br>- ^ 异或 当左右表达式结果不同时 为<span class="hljs-literal">true</span> 否则为<span class="hljs-literal">false</span><br><br>  - ```java<br>    <span class="hljs-built_in">int</span> a = <span class="hljs-number">10</span><br>    <span class="hljs-built_in">int</span> b = <span class="hljs-number">20</span><br>    Syteam.<span class="hljs-keyword">out</span>.println((a &gt; b) ^ (a &lt; b)) <span class="hljs-comment">//true</span><br>    Syteam.<span class="hljs-keyword">out</span>.println((a &gt; <span class="hljs-number">9</span>) ^ (b &lt; <span class="hljs-number">19</span>)) <span class="hljs-comment">//false</span><br></code></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h2 id="进制"><a href="#进制" class="headerlink" title="进制"></a>进制</h2><h3 id="进制的互相转换"><a href="#进制的互相转换" class="headerlink" title="进制的互相转换"></a>进制的互相转换</h3><h4 id="其他进制转十进制"><a href="#其他进制转十进制" class="headerlink" title="其他进制转十进制"></a>其他进制转十进制</h4><ul>
<li>二进制转十进制<ul>
<li>从最低位(右)开始,将每个位上的数提取出来 , 乘以<strong>2</strong>的(位数-1)次方,然后求和</li>
</ul>
</li>
<li>八进制转十进制<ul>
<li>从最低位(右)开始,将每个位上的数提取出来 , 乘以<strong>8</strong>的(位数-1)次方,然后求和</li>
</ul>
</li>
<li>16进制转十进制<ul>
<li>从最低位(右边)开始，将每个位上的数提取出来，乘以 <strong>16</strong> 的(位数-1)次方，然后求和</li>
</ul>
</li>
</ul>
<h4 id="十进制转成其他进制"><a href="#十进制转成其他进制" class="headerlink" title="十进制转成其他进制"></a>十进制转成其他进制</h4><ul>
<li>十进制转二进制<ul>
<li>将该数不断除以 2，直到商为 0 为止，然后将每步得到的余数倒过来，就是对应的二进制</li>
<li>注意一个字节有6位 需要用0来填充高位(0b的后面添0)</li>
</ul>
</li>
<li>十进制转八进制<ul>
<li>将该数不断除以 8，直到商为 0 为止，然后将每步得到的余数倒过来，就是对应的八进制</li>
</ul>
</li>
<li>十进制转十六进制<ul>
<li>将该数不断除以 16，直到商为 0 为止，然后将每步得到的余数倒过来，就是对应的十六进制。</li>
</ul>
</li>
</ul>
<h4 id="二进制与十六进制八进制互相转换"><a href="#二进制与十六进制八进制互相转换" class="headerlink" title="二进制与十六进制八进制互相转换"></a>二进制与十六进制八进制互相转换</h4><ul>
<li>二进制转八进制<ul>
<li>从低位开始,将二进制数每三位一组，转成对应的八进制数即可。</li>
</ul>
</li>
<li>2转16进制<ul>
<li>从低位开始，将二进制数每四位一组，转成对应的十六进制数即可。</li>
</ul>
</li>
<li>8转2<ul>
<li>将八进制数每 1 位，转成对应的一个 3 位的二进制数即可</li>
</ul>
</li>
<li>16转2<ul>
<li>将十六进制数每 1 位，转成对应的 4 位的一个二进制数即可</li>
</ul>
</li>
</ul>
<h2 id="引用类型"><a href="#引用类型" class="headerlink" title="引用类型"></a>引用类型</h2><h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><h4 id=""><a href="#" class="headerlink" title=""></a></h4><h4 id="二维数组的使用"><a href="#二维数组的使用" class="headerlink" title="二维数组的使用"></a>二维数组的使用</h4><p><strong>语法</strong></p>
<ul>
<li>&#96;&#96;&#96;<br>类型[][] 数组名 &#x3D; new 类型[大小][大小]<br>第一个是大小是父级长度 第二个大小是元素数组长度<br>比如:<br>int a[][] &#x3D; new int[2][3]<figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs haxe"><br>  <br><br><br><br><span class="hljs-meta">### 对象</span><br><br>**定义一个对象**<br><br>```java<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Man</span> </span>&#123;<br>    <span class="hljs-keyword">String</span> name<br>    int age<br>    <span class="hljs-keyword">String</span> sex<br>&#125;<br><br><span class="hljs-comment">//创造一个对象变量</span><br>Man man1 = <span class="hljs-keyword">new</span> <span class="hljs-type">Man</span>()<br>man1.name = <span class="hljs-string">&#x27;邓子雄&#x27;</span><br>    ...<br></code></pre></td></tr></table></figure></li>
</ul>
<p><strong>注意点</strong></p>
<ul>
<li>属性不赋值 有默认值</li>
</ul>
<h2 id="类"><a href="#类" class="headerlink" title="类"></a>类</h2><ul>
<li>属性\方法\构造器\代码块\内部类  类的五大成员</li>
</ul>
<h3 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h3><p><strong>方法重载</strong></p>
<ul>
<li>方法名:必须相同</li>
<li>形参列表:必须不同(形参类型或个数或顺序不同,参数名无要求. 但是相同类型不同参数名不能触发重载)</li>
<li>返回类型无要求</li>
</ul>
<h3 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h3><p><strong>注意点:</strong></p>
<ul>
<li>构造器没有返回值 不用写viod</li>
<li>构造器名称和类名一样</li>
<li>构造器也可触发重载</li>
<li>构造器是对象初始化 不是创造对象</li>
<li>没有构造器 会给一个默认构造器 无参数 (有构造器后.默认构造器消失)</li>
</ul>
<p><strong>对象初始化流程:</strong></p>
<ol>
<li>加载类信息</li>
<li>在堆中分配空间</li>
<li>完成对象初始化<ol>
<li>默认初始化 (给类全局变量默认值..)</li>
<li>显示初始化 (给初始变量赋值)</li>
<li>构造器初始化</li>
</ol>
</li>
<li>把堆中对象的地址暴露给外界</li>
</ol>
<p><strong>this</strong></p>
<ul>
<li>this可以访问本类的属性 方法 变量</li>
<li>this用于区分当前类的属性和局部变量</li>
<li>this.方法名()</li>
<li>访问构造器  this(参数…)</li>
<li>this不能再外部使用 只能在类定义的方法中使用</li>
</ul>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p><strong>注意点:</strong></p>
<ul>
<li><p>私有的属性访问需要用公共方法</p>
</li>
<li><p>new子类会自动调用super() -&gt;调用父类的无参构造器</p>
</li>
<li><p>指定调用父类构造器</p>
<ul>
<li>&#96;&#96;&#96;java<br>class A {<br>public A() {<br>    &#x2F;&#x2F;调用无参构造器就不穿参数<br>    super(…参数列表)<br>}<br>}<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs markdown"><br><span class="hljs-bullet">-</span> 继承最多只能继承一个父类<br><br><br><br><span class="hljs-section">#### super和this的区别</span><br><br>super使用时优先查找父类 ,  this优先查找本类.  如果没有就逐级查找<br><br><br><br><br><br><span class="hljs-section">### 内部类</span><br><br><span class="hljs-strong">**内部类的分类**</span><br><br><span class="hljs-bullet">-</span> 定义在外部类局部位置上(比如方法内)<br><span class="hljs-bullet">  -</span> 局部内部类(有类名)<br><span class="hljs-bullet">  -</span> 匿名内部类(没有类名)<br><span class="hljs-bullet">-</span> 定义在外部类的成员位置上:<br><span class="hljs-bullet">  -</span> 成员内部类(没有static修饰)<br><span class="hljs-bullet">  -</span> 静态内部类(使用static修饰)<br><br><br><br><span class="hljs-section">#### 局部内部类</span><br><br><span class="hljs-strong">**说明:**</span> 局部内部类是定义在外部类的局部位置，比如方法中，并且有类名。<br><br><span class="hljs-bullet">-</span> 可以直接访问外部类的所有成员，包含私有的<br><br><span class="hljs-bullet">-</span> 不能添加访问修饰符，因为它的地位就是一 个局部变量。局部变量是不能使用修饰符的. 但是可以用final修饰 因为局部变量也可以使用final<br><br><span class="hljs-bullet">-</span> 作用域: 仅仅在定义它的方法或代码块中。<br><br><span class="hljs-bullet">-</span> 如果外部类和局部内部类的成员重名时，默认遵循就近原则，如果想访问外部类的成员，则可以使用(外部类名.this.成员) 去访问<br><br>  <span class="hljs-code">```java</span><br><span class="hljs-code">  System.out.println(&quot;n1=&quot; + n1 + &quot; 外部类的 n1=&quot; + Outer02.this.n1);</span><br></code></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h4 id="匿名内部类"><a href="#匿名内部类" class="headerlink" title="匿名内部类"></a>匿名内部类</h4><p><strong>说明:</strong>  定义在外部类的局部位置 (方法,代码块) 并且没有类名</p>
<p><strong>语法:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//new 类或接口(参数列表)&#123;</span><br><span class="hljs-comment">//		类体</span><br><span class="hljs-comment">//&#125;</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">A</span> &#123;<br>    <span class="hljs-type">int</span> <span class="hljs-variable">Num</span> <span class="hljs-operator">=</span> <span class="hljs-number">99</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">m1</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">//匿名内部类</span><br>        <span class="hljs-type">A</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">A</span>()&#123;<br>            .....<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>本质是类</li>
<li>内部类</li>
<li>类没有名字(或者是[new的类名]+ $1 or 2 or 3….)</li>
<li>是一个对象</li>
</ul>
<p><strong>最佳实践</strong></p>
<p>当作参数传递</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">InnerClassExercise02</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-comment">/*</span><br><span class="hljs-comment">        1.有一个铃声接口Bell，里面有个ring方法。(右图)</span><br><span class="hljs-comment">        2.有一个手机类Cellphone，具有闹钟功能alarmClock，参数是Bell类型(右图)</span><br><span class="hljs-comment">        3.测试手机类的闹钟功能，通过匿名内部类(对象)作为参数，打印：懒猪起床了</span><br><span class="hljs-comment">        4.再传入另一个匿名内部类(对象)，打印：小伙伴上课了</span><br><span class="hljs-comment">         */</span><br>        <span class="hljs-type">CellPhone</span> <span class="hljs-variable">cellPhone</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CellPhone</span>();<br>        <span class="hljs-comment">//老韩解读</span><br>        <span class="hljs-comment">//1. 传递的是实现了 Bell接口的匿名内部类 InnerClassExercise02$1</span><br>        <span class="hljs-comment">//2. 重写了 ring</span><br>        <span class="hljs-comment">//3. Bell bell = new Bell() &#123;</span><br>        <span class="hljs-comment">//            @Override</span><br>        <span class="hljs-comment">//            public void ring() &#123;</span><br>        <span class="hljs-comment">//                System.out.println(&quot;懒猪起床了&quot;);</span><br>        <span class="hljs-comment">//            &#125;</span><br>        <span class="hljs-comment">//        &#125;</span><br>        cellPhone.alarmClock(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Bell</span>() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">ring</span><span class="hljs-params">()</span> &#123;<br>                System.out.println(<span class="hljs-string">&quot;懒猪起床了&quot;</span>);<br>            &#125;<br>        &#125;);<br><br>        cellPhone.alarmClock(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Bell</span>() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">ring</span><span class="hljs-params">()</span> &#123;<br>                System.out.println(<span class="hljs-string">&quot;小伙伴上课了&quot;</span>);<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Bell</span>&#123; <span class="hljs-comment">//接口</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">ring</span><span class="hljs-params">()</span>;<span class="hljs-comment">//方法</span><br>&#125;<br><span class="hljs-keyword">class</span> <span class="hljs-title class_">CellPhone</span>&#123;<span class="hljs-comment">//类</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">alarmClock</span><span class="hljs-params">(Bell bell)</span>&#123;<span class="hljs-comment">//形参是Bell接口类型</span><br>        System.out.println(bell.getClass());<br>        bell.ring();<span class="hljs-comment">//动态绑定</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>





<h4 id="成员内部类"><a href="#成员内部类" class="headerlink" title="成员内部类"></a>成员内部类</h4><p><strong>说明:</strong>  成员内部类是定义再外部类的成员位置, 并且没有static修饰.</p>
<p><strong>特性:</strong></p>
<ul>
<li>可以直接访问外部类的所有成员, 包含私有的</li>
<li>可以添加任意修饰符</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MemberInnerClass01</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Outer08</span> <span class="hljs-variable">outer08</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Outer08</span>();<br>        outer08.t1();<br><br>        <span class="hljs-comment">//外部其他类，使用成员内部类的三种方式</span><br>        <span class="hljs-comment">//老韩解读</span><br>        <span class="hljs-comment">// 第一种方式</span><br>        <span class="hljs-comment">// outer08.new Inner08(); 相当于把 new Inner08()当做是outer08成员</span><br>        <span class="hljs-comment">// 这就是一个语法，不要特别的纠结.</span><br>        Outer08.<span class="hljs-type">Inner08</span> <span class="hljs-variable">inner08</span> <span class="hljs-operator">=</span> outer08.<span class="hljs-keyword">new</span> <span class="hljs-title class_">Inner08</span>();<br>        inner08.say();<br>        <span class="hljs-comment">// 第二方式 在外部类中，编写一个方法，可以返回 Inner08对象</span><br>        Outer08.<span class="hljs-type">Inner08</span> <span class="hljs-variable">inner08Instance</span> <span class="hljs-operator">=</span> outer08.getInner08Instance();<br>        inner08Instance.say();<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Outer08</span> &#123; <span class="hljs-comment">//外部类</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> <span class="hljs-variable">n1</span> <span class="hljs-operator">=</span> <span class="hljs-number">10</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;张三&quot;</span>;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">hi</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;hi()方法...&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">//1.注意: 成员内部类，是定义在外部内的成员位置上</span><br>    <span class="hljs-comment">//2.可以添加任意访问修饰符(public、protected 、默认、private),因为它的地位就是一个成员</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Inner08</span> &#123;<span class="hljs-comment">//成员内部类</span><br>        <span class="hljs-keyword">private</span> <span class="hljs-type">double</span> <span class="hljs-variable">sal</span> <span class="hljs-operator">=</span> <span class="hljs-number">99.8</span>;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> <span class="hljs-variable">n1</span> <span class="hljs-operator">=</span> <span class="hljs-number">66</span>;<br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">say</span><span class="hljs-params">()</span> &#123;<br>            <span class="hljs-comment">//可以直接访问外部类的所有成员，包含私有的</span><br>            <span class="hljs-comment">//如果成员内部类的成员和外部类的成员重名，会遵守就近原则.</span><br>            <span class="hljs-comment">//，可以通过  外部类名.this.属性 来访问外部类的成员</span><br>            System.out.println(<span class="hljs-string">&quot;n1 = &quot;</span> + n1 + <span class="hljs-string">&quot; name = &quot;</span> + name + <span class="hljs-string">&quot; 外部类的n1=&quot;</span> + Outer08.<span class="hljs-built_in">this</span>.n1);<br>            hi();<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">//方法，返回一个Inner08实例</span><br>    <span class="hljs-keyword">public</span> Inner08 <span class="hljs-title function_">getInner08Instance</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Inner08</span>();<br>    &#125;<br><br><br>    <span class="hljs-comment">//写方法</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">t1</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">//使用成员内部类</span><br>        <span class="hljs-comment">//创建成员内部类的对象，然后使用相关的方法</span><br>        <span class="hljs-type">Inner08</span> <span class="hljs-variable">inner08</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Inner08</span>();<br>        inner08.say();<br>        System.out.println(inner08.sal);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>







<h4 id="静态内部类"><a href="#静态内部类" class="headerlink" title="静态内部类"></a>静态内部类</h4><p><strong>说明:</strong>  成员内部类是定义再外部类的成员位置, 并且有static修饰.</p>
<p><strong>特性:</strong></p>
<ul>
<li>可以直接访问外部类的所有静态成员，包含私有的，但不能直接访问非静态成员</li>
<li>可以添加任意访问修饰符(public protected 、默认、private),因为它的地位就是一个成员</li>
<li>作用域:同其他的成员，为整个类体</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StaticInnerClass01</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Outer10</span> <span class="hljs-variable">outer10</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Outer10</span>();<br>        outer10.m1();<br><br>        <span class="hljs-comment">//外部其他类 使用静态内部类</span><br>        <span class="hljs-comment">//方式1</span><br>        <span class="hljs-comment">//因为静态内部类，是可以通过类名直接访问(前提是满足访问权限)</span><br>        Outer10.<span class="hljs-type">Inner10</span> <span class="hljs-variable">inner10</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Outer10</span>.Inner10();<br>        inner10.say();<br>        <span class="hljs-comment">//方式2</span><br>        <span class="hljs-comment">//编写一个方法，可以返回静态内部类的对象实例.</span><br>        Outer10.<span class="hljs-type">Inner10</span> <span class="hljs-variable">inner101</span> <span class="hljs-operator">=</span> outer10.getInner10();<br>        System.out.println(<span class="hljs-string">&quot;============&quot;</span>);<br>        inner101.say();<br><br>        Outer10.<span class="hljs-type">Inner10</span> <span class="hljs-variable">inner10_</span> <span class="hljs-operator">=</span> Outer10.getInner10_();<br>        System.out.println(<span class="hljs-string">&quot;************&quot;</span>);<br>        inner10_.say();<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Outer10</span> &#123; <span class="hljs-comment">//外部类</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> <span class="hljs-variable">n1</span> <span class="hljs-operator">=</span> <span class="hljs-number">10</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;张三&quot;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">cry</span><span class="hljs-params">()</span> &#123;&#125;<br>    <span class="hljs-comment">//Inner10就是静态内部类</span><br>    <span class="hljs-comment">//1. 放在外部类的成员位置</span><br>    <span class="hljs-comment">//2. 使用static 修饰</span><br>    <span class="hljs-comment">//3. 可以直接访问外部类的所有静态成员，包含私有的，但不能直接访问非静态成员</span><br>    <span class="hljs-comment">//4. 可以添加任意访问修饰符(public、protected 、默认、private),因为它的地位就是一个成员</span><br>    <span class="hljs-comment">//5. 作用域 ：同其他的成员，为整个类体</span><br>    <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Inner10</span> &#123;<br>        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;韩顺平教育&quot;</span>;<br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">say</span><span class="hljs-params">()</span> &#123;<br>            <span class="hljs-comment">//如果外部类和静态内部类的成员重名时，静态内部类访问的时，</span><br>            <span class="hljs-comment">//默认遵循就近原则，如果想访问外部类的成员，则可以使用 （外部类名.成员）</span><br>            System.out.println(name + <span class="hljs-string">&quot; 外部类name= &quot;</span> + Outer10.name);<br>            cry();<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">m1</span><span class="hljs-params">()</span> &#123; <span class="hljs-comment">//外部类---访问------&gt;静态内部类 访问方式：创建对象，再访问</span><br>        <span class="hljs-type">Inner10</span> <span class="hljs-variable">inner10</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Inner10</span>();<br>        inner10.say();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Inner10 <span class="hljs-title function_">getInner10</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Inner10</span>();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Inner10 <span class="hljs-title function_">getInner10_</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Inner10</span>();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>





<h3 id="包装类"><a href="#包装类" class="headerlink" title="包装类"></a>包装类</h3><h3 id="Integer"><a href="#Integer" class="headerlink" title="Integer"></a>Integer</h3><p><strong>注意点:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">WrapperExercise02</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Integer</span>(<span class="hljs-number">1</span>);<br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Integer</span>(<span class="hljs-number">1</span>);<br>        System.out.println(i == j);  <span class="hljs-comment">//False</span><br>        <span class="hljs-comment">//所以，这里主要是看范围 -128 ~ 127 就是直接返回</span><br>        <span class="hljs-comment">/*</span><br><span class="hljs-comment">       </span><br><span class="hljs-comment">        //1. 如果i 在 IntegerCache.low(-128)~IntegerCache.high(127),就直接从数组返回</span><br><span class="hljs-comment">        //2. 如果不在 -128~127,就直接 new Integer(i)</span><br><span class="hljs-comment">         public static Integer valueOf(int i) &#123;</span><br><span class="hljs-comment">            if (i &gt;= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)</span><br><span class="hljs-comment">                return IntegerCache.cache[i + (-IntegerCache.low)];</span><br><span class="hljs-comment">            return new Integer(i);</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">         */</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">m</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; <span class="hljs-comment">//底层 Integer.valueOf(1); -&gt; 阅读源码</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<span class="hljs-comment">//底层 Integer.valueOf(1);</span><br>        System.out.println(m == n); <span class="hljs-comment">//T</span><br>        <span class="hljs-comment">//所以，这里主要是看范围 -128 ~ 127 就是直接返回</span><br>        <span class="hljs-comment">//，否则，就new Integer(xx);</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">x</span> <span class="hljs-operator">=</span> <span class="hljs-number">128</span>;<span class="hljs-comment">//底层Integer.valueOf(1);</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">y</span> <span class="hljs-operator">=</span> <span class="hljs-number">128</span>;<span class="hljs-comment">//底层Integer.valueOf(1);</span><br>        System.out.println(x == y);<span class="hljs-comment">//False</span><br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>





<h3 id="String类"><a href="#String类" class="headerlink" title="String类"></a>String类</h3><p><strong>常用构造器:</strong></p>
<ul>
<li>String s1 &#x3D; new String(); </li>
<li>String s2 &#x3D; new String(String original);</li>
<li>String s3 &#x3D; new String(char[] a);</li>
<li>String s4 &#x3D; new String(char[] a,int startIndex,int count)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">String01</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-comment">//1.String 对象用于保存字符串，也就是一组字符序列</span><br>        <span class="hljs-comment">//2. &quot;jack&quot; 字符串常量, 双引号括起的字符序列</span><br>        <span class="hljs-comment">//3. 字符串的字符使用Unicode字符编码，一个字符(不区分字母还是汉字)占两个字节</span><br>        <span class="hljs-comment">//4. String 类有很多构造器，构造器的重载</span><br>        <span class="hljs-comment">//   常用的有 String  s1 = new String(); //</span><br>        <span class="hljs-comment">//String  s2 = new String(String original);</span><br>        <span class="hljs-comment">//String  s3 = new String(char[] a);</span><br>        <span class="hljs-comment">//String  s4 =  new String(char[] a,int startIndex,int count)</span><br>        <span class="hljs-comment">//String s5 = new String(byte[] b)</span><br>        <span class="hljs-comment">//5. String 类实现了接口 Serializable【String 可以串行化:可以在网络传输】</span><br>        <span class="hljs-comment">//                 接口 Comparable [String 对象可以比较大小]</span><br>        <span class="hljs-comment">//6. String 是final 类，不能被其他的类继承</span><br>        <span class="hljs-comment">//7. String 有属性 private final char value[]; 用于存放字符串内容</span><br>        <span class="hljs-comment">//8. value 是一个final类型， 不可以修改(需要功力)：即value不能指向</span><br>        <span class="hljs-comment">//   新的地址，但是单个字符内容是可以变化</span><br><br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;jack&quot;</span>;<br>        name = <span class="hljs-string">&quot;tom&quot;</span>;<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">char</span>[] value = &#123;<span class="hljs-string">&#x27;a&#x27;</span>,<span class="hljs-string">&#x27;b&#x27;</span>,<span class="hljs-string">&#x27;c&#x27;</span>&#125;;<br>        <span class="hljs-type">char</span>[] v2 = &#123;<span class="hljs-string">&#x27;t&#x27;</span>,<span class="hljs-string">&#x27;o&#x27;</span>,<span class="hljs-string">&#x27;m&#x27;</span>&#125;;<br>        value[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;H&#x27;</span>;<br>        <span class="hljs-comment">//value = v2; 不可以修改 value地址</span><br><br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>



<p><strong>String s &#x3D; “dzx” 与 String s &#x3D; new String(“dzx”)的区别</strong></p>
<ol>
<li>方式:先从常量池查看是否有”dzx”数据空间，如果有，直接指向;如果没有则重新创建，然后指向。s最终指向的是常量池的空间地址</li>
<li>方式二:先在堆中创建空间，里面维护了value属性，指向常量池的dzx空间如果常量池没有”dzx”,重新创建，如果有，直接通过value指向。最终指向的是堆中的空间地址。</li>
</ol>
<p><strong>注意点:</strong></p>
<ul>
<li><p>string.intern()方法 最终返回的是常量池的地址</p>
</li>
<li><p>&#96;&#96;&#96;java<br>String a &#x3D; “dzx” + “niub”<br>&#x2F;&#x2F;编译器会优化为<br>String a &#x3D; “dzxniub”</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs awk"><br>- ```java<br>  String a = <span class="hljs-string">&quot;hello&quot;</span>;<br>  String b = <span class="hljs-string">&quot;abc&quot;</span>;<br>  String c = a + b;  <span class="hljs-regexp">//</span>创建了几个对象<br>  <br>  <span class="hljs-regexp">/* 答 */</span><br>  <span class="hljs-regexp">//</span>底层是StringBuilder sb =  new StringBuilder(); sb.append(a) sb.append(b); sb是在堆中，并且append是在原来字符串的基础 上追加的.<br>  <span class="hljs-regexp">//</span> 所以 常量相加看的是常量池,  变量相加看的是堆<br>  <br></code></pre></td></tr></table></figure></li>
</ul>
<h4 id="StringBuffer"><a href="#StringBuffer" class="headerlink" title="StringBuffer"></a>StringBuffer</h4><p><strong>定义</strong></p>
<ul>
<li>可变的字符序列,可以对字符串内容进行增删.</li>
</ul>
<p><strong>注意点:</strong></p>
<ul>
<li>StringBuffer 的直接父类 是 AbstractStringBuilder</li>
<li>StringBuffer 实现了 Serializable, 即 StringBuffer 的对象可以串行化</li>
<li>在父类中 AbstractStringBuilder 有属性 char[] value,不是 final, 该 value 数组存放 字符串内容，引出存放在堆中的</li>
<li>StringBuffer 是一个 final 类，不能被继承</li>
<li>因为 StringBuffer 字符内容是存在 char[] value, 所有在变化(增加&#x2F;删除),  不用每次都更换地址(即不是每次创建新对象)， 所以效率高于 String</li>
</ul>
<h5 id="StringBuffer-和-String相互转换"><a href="#StringBuffer-和-String相互转换" class="headerlink" title="StringBuffer 和 String相互转换"></a>StringBuffer 和 String相互转换</h5><p><strong>String —-&gt; StringBuffer:</strong> </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">str</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;hello tom&quot;</span>;<br><span class="hljs-comment">//方式 1 使用构造器</span><br><span class="hljs-comment">//注意： 返回的才是 StringBuffer 对象，对 str 本身没有影响</span><br><span class="hljs-type">StringBuffer</span> <span class="hljs-variable">stringBuffer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>(str);<br><br><span class="hljs-comment">//方式 2 使用的是 append 方法</span><br><span class="hljs-type">StringBuffer</span> <span class="hljs-variable">stringBuffer1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>();<br>stringBuffer1 = stringBuffer1.append(str);<br><br></code></pre></td></tr></table></figure>



<p><strong>StringBuffer —-&gt; String:</strong> </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">StringBuffer</span> <span class="hljs-variable">stringBuffer3</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>(<span class="hljs-string">&quot;韩顺平教育&quot;</span>);<br><span class="hljs-comment">//方式 1 使用 StringBuffer 提供的 toString 方法</span><br><span class="hljs-type">String</span> <span class="hljs-variable">s</span> <span class="hljs-operator">=</span> stringBuffer3.toString();<br><br><span class="hljs-comment">//方式 2: 使用构造器来搞定</span><br><span class="hljs-type">String</span> <span class="hljs-variable">s1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(stringBuffer3);<br><br></code></pre></td></tr></table></figure>

<h4 id="String-、StringBuffer-StringBuilder的比较"><a href="#String-、StringBuffer-StringBuilder的比较" class="headerlink" title="String 、StringBuffer  StringBuilder的比较"></a>String 、StringBuffer  StringBuilder的比较</h4><ol>
<li>String  不可变字符序列, 效率低  复用率高</li>
<li>StringBuffer  可变字符序列  效率较高(增删)  线程安全</li>
<li>StringBuilder  可变字符序列  效率最高 线程不安全</li>
</ol>
<h4 id="String、StringBuffer-和-StringBuilder-的选择"><a href="#String、StringBuffer-和-StringBuilder-的选择" class="headerlink" title="String、StringBuffer 和 StringBuilder 的选择"></a>String、StringBuffer 和 StringBuilder 的选择</h4><ul>
<li>如果字符串存在大量的修改操作，一般使用StringBuffer或StringBuilder</li>
<li>如果字符串存在大量的修改操作，并在<strong>单线程</strong>的情况，使用StringBuilder</li>
<li>如果字符串存在大量的修改操作，并在<strong>多线程</strong>的情况，使用StringBuffer</li>
<li>如果我们字符串<strong>很少修改</strong>，被多个对象引用，使用String, 比如配置信息等</li>
</ul>
<h3 id="Array类"><a href="#Array类" class="headerlink" title="Array类"></a>Array类</h3><p><strong>Array的一些方法:</strong></p>
<ul>
<li><p>sort</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//sort 定制写法  (匿名内部类)</span><br>Arrays.sort(arr, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Comparator</span>() &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compare</span><span class="hljs-params">(Object o1, Object o2)</span> &#123;<br>        <span class="hljs-comment">//前一项</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">i1</span> <span class="hljs-operator">=</span> (Integer) o1;<br>        <span class="hljs-comment">//后一项</span><br>        <span class="hljs-type">Integer</span> <span class="hljs-variable">i2</span> <span class="hljs-operator">=</span> (Integer) o2;<br>        <span class="hljs-comment">//底层通过判断返回值是否大于0 来决定是升序还是降序</span><br>        <span class="hljs-keyword">return</span> i2 - i1;<br>    &#125;<br>&#125;);<br></code></pre></td></tr></table></figure>

<p><strong>冒泡排序:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java">bubble(arr, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Comparator</span>() &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compare</span><span class="hljs-params">(Object o1, Object o2)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">i1</span> <span class="hljs-operator">=</span> (Integer) o1;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">i2</span> <span class="hljs-operator">=</span> (Integer) o2;<br>        <span class="hljs-keyword">return</span> i2 - i1;<span class="hljs-comment">// return i2 - i1;</span><br>    &#125;<br>&#125;);<br><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">bubble</span><span class="hljs-params">(<span class="hljs-type">int</span>[] arr, Comparator c)</span> &#123;<br>    <span class="hljs-comment">//中间变量</span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; arr.length - <span class="hljs-number">1</span>; i++) &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; arr.length - <span class="hljs-number">1</span> - i; j++) &#123;<br>        <span class="hljs-comment">//数组排序由 c.compare(arr[j], arr[j + 1])返回的值决定</span><br>            <span class="hljs-keyword">if</span> (c.compare(arr[j], arr[j + <span class="hljs-number">1</span>]) &gt; <span class="hljs-number">0</span>) &#123;<br>                <span class="hljs-comment">//交换前后项在数组中的位置</span><br>                temp = arr[j];<br>                arr[j] = arr[j + <span class="hljs-number">1</span>];<br>                arr[j + <span class="hljs-number">1</span>] = temp;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>binarySearch  二叉查找</p>
<ul>
<li>要求该数组是有序的. 如果该数组是无序的，不能使用 binarySearch</li>
<li>如果数组中不存在该元素，就返回插入点值.  **插入点:**第一个比关键字大的元素在数组中的位置索引，而且这个位置索引从1开始。</li>
</ul>
</li>
</ul>
<h3 id="BigInteger类"><a href="#BigInteger类" class="headerlink" title="BigInteger类"></a>BigInteger类</h3><p><strong>当我们编程中，需要处理很大的整数，long 不够用,  可以使用 BigInteger 的类来搞定</strong></p>
<p><strong>注意点:</strong></p>
<ul>
<li>在对 BigInteger 进行加减乘除的时候，需要使用对应的方法，不能直接进行 + - * &#x2F;</li>
</ul>
<h3 id="BigDecimal类"><a href="#BigDecimal类" class="headerlink" title="BigDecimal类"></a>BigDecimal类</h3><p><strong>当我们需要保存一个精度很高的数时，double 不够用,  可以可以使用 BigDecimal</strong> </p>
<p><strong>注意点:</strong></p>
<ul>
<li>在对 BigDecimal进行加减乘除的时候，需要使用对应的方法，不能直接进行 + - * &#x2F;</li>
</ul>
<h3 id="Date日期类-第一代"><a href="#Date日期类-第一代" class="headerlink" title="Date日期类(第一代)"></a>Date日期类(第一代)</h3><p><strong>精确到毫秒,代表特定的瞬间</strong></p>
<p><strong>注意点:</strong></p>
<ul>
<li>默认输出的日期格式是国外的方式, 因此通常需要对格式进行转换</li>
</ul>
<h4 id="SimpleDateFormat-Date日期的转换"><a href="#SimpleDateFormat-Date日期的转换" class="headerlink" title="SimpleDateFormat   Date日期的转换"></a>SimpleDateFormat   Date日期的转换</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Date</span> <span class="hljs-variable">d1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(); <span class="hljs-comment">//获取当前系统时间</span><br><br><span class="hljs-type">SimpleDateFormat</span> <span class="hljs-variable">sdf</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleDateFormat</span>(<span class="hljs-string">&quot;yyyy 年 MM 月 dd 日 hh:mm:ss E&quot;</span>);<br><br><span class="hljs-type">String</span> <span class="hljs-variable">format</span> <span class="hljs-operator">=</span> sdf.format(d1); <span class="hljs-comment">// format:将日期转换成指定格式的字符串</span><br></code></pre></td></tr></table></figure>





<h3 id="Calendar日历类-二代"><a href="#Calendar日历类-二代" class="headerlink" title="Calendar日历类(二代)"></a>Calendar日历类(二代)</h3><p><strong>注意点:</strong></p>
<ul>
<li>Calendar 是一个抽象类， 并且构造器是 private</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//4. Calendar 没有提供对应的格式化的类，因此需要程序员自己组合来输出</span><br><span class="hljs-comment">//5. 如果我们需要按照 24 小时进制来获取时间， Calendar.HOUR ==改成=&gt; Calendar.HOUR_OF_DAY</span><br><span class="hljs-type">Calendar</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> Calendar.getInstance(); <span class="hljs-comment">//创建日历类对象</span><br>System.out.println(<span class="hljs-string">&quot;c=&quot;</span> + c);<br><span class="hljs-comment">//2.获取日历对象的某个日历字段</span><br>System.out.println(<span class="hljs-string">&quot;年：&quot;</span> + c.get(Calendar.YEAR));<br><span class="hljs-comment">// 这里为什么要 + 1, 因为 Calendar 返回月时候，是按照 0 开始编号</span><br>System.out.println(<span class="hljs-string">&quot;月：&quot;</span> + (c.get(Calendar.MONTH) + <span class="hljs-number">1</span>));<br>System.out.println(<span class="hljs-string">&quot;日：&quot;</span> + c.get(Calendar.DAY_OF_MONTH));<br>System.out.println(<span class="hljs-string">&quot;小时：&quot;</span> + c.get(Calendar.HOUR));<br>System.out.println(<span class="hljs-string">&quot;分钟：&quot;</span> + c.get(Calendar.MINUTE));<br>System.out.println(<span class="hljs-string">&quot;秒：&quot;</span> + c.get(Calendar.SECOND));<br><span class="hljs-comment">//Calender 没有专门的格式化方法，所以需要程序员自己来组合显示</span><br></code></pre></td></tr></table></figure>



<h3 id="第三代日期类"><a href="#第三代日期类" class="headerlink" title="第三代日期类"></a>第三代日期类</h3><ul>
<li>LocalDate  只包含日期,</li>
<li>LocalTime 只包含时间</li>
<li>LocalDateTime  包含日期和时间</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//1. 使用 now() 返回表示当前日期时间的 对象</span><br><span class="hljs-type">LocalDateTime</span> <span class="hljs-variable">ldt</span> <span class="hljs-operator">=</span> LocalDateTime.now(); <span class="hljs-comment">//LocalDate.now();//LocalTime.now()</span><br>System.out.println(ldt);<br><span class="hljs-comment">//2. 使用 DateTimeFormatter 对象来进行格式化</span><br><span class="hljs-comment">// 创建 DateTimeFormatter 对象</span><br><span class="hljs-type">DateTimeFormatter</span> <span class="hljs-variable">dateTimeFormatter</span> <span class="hljs-operator">=</span> DateTimeFormatter.ofPattern(<span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<br><br><span class="hljs-type">String</span> <span class="hljs-variable">format</span> <span class="hljs-operator">=</span> dateTimeFormatter.format(ldt);<br>System.out.println(<span class="hljs-string">&quot;格式化的日期=&quot;</span> + format);<br><br><span class="hljs-comment">//获取年月日时分秒</span><br>System.out.println(<span class="hljs-string">&quot;年=&quot;</span> + ldt.getYear());<br>System.out.println(<span class="hljs-string">&quot;月=&quot;</span> + ldt.getMonth());<br>System.out.println(<span class="hljs-string">&quot;月=&quot;</span> + ldt.getMonthValue());<br>System.out.println(<span class="hljs-string">&quot;日=&quot;</span> + ldt.getDayOfMonth());<br>System.out.println(<span class="hljs-string">&quot;时=&quot;</span> + ldt.getHour());<br>System.out.println(<span class="hljs-string">&quot;分=&quot;</span> + ldt.getMinute());<br>System.out.println(<span class="hljs-string">&quot;秒=&quot;</span> + ldt.getSecond());<br><br></code></pre></td></tr></table></figure>



<h3 id="Instant-时间戳"><a href="#Instant-时间戳" class="headerlink" title="Instant 时间戳"></a>Instant 时间戳</h3><hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//1.通过 静态方法 now() 获取表示当前时间戳的对象</span><br><span class="hljs-type">Instant</span> <span class="hljs-variable">now</span> <span class="hljs-operator">=</span> Instant.now();<br>System.out.println(now);<br><br><span class="hljs-comment">//2. 通过 from 可以把 Instant 转成 Date</span><br><span class="hljs-type">Date</span> <span class="hljs-variable">date</span> <span class="hljs-operator">=</span> Date.from(now);<br><br><span class="hljs-comment">//3. 通过 date 的 toInstant() 可以把 date 转成 Instant 对象</span><br><span class="hljs-type">Instant</span> <span class="hljs-variable">instant</span> <span class="hljs-operator">=</span> date.toInstant();<br></code></pre></td></tr></table></figure>





<hr>
<h3 id="枚举类"><a href="#枚举类" class="headerlink" title="枚举类"></a>枚举类</h3><p><strong>枚举类的几种继承原生方法:</strong></p>
<ol>
<li>toString:Enum 类已经重写过了，返回的是当前对象名,子类可以重写该方法，用于返回对象的属性信息</li>
<li>name：返回当前对象名（常量名），子类中不能重写</li>
<li>ordinal：返回当前对象的位置号，默认从 0 开始</li>
<li>values：返回当前枚举类中所有的常量</li>
<li>valueOf：将字符串转换成枚举对象，要求字符串必须为已有的常量名，否则报异常</li>
<li>compareTo：比较两个枚举常量，比较的就是编号</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">EnumMethod</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-comment">//使用Season2 枚举类，来演示各种方法</span><br>        <span class="hljs-type">Season2</span> <span class="hljs-variable">autumn</span> <span class="hljs-operator">=</span> Season2.AUTUMN;<br><br>        <span class="hljs-comment">//输出枚举对象的名字</span><br>        System.out.println(autumn.name());<br>        <span class="hljs-comment">//ordinal() 输出的是该枚举对象的次序/编号，从0开始编号</span><br>        <span class="hljs-comment">//AUTUMN 枚举对象是第三个，因此输出 2</span><br>        System.out.println(autumn.ordinal());<br>        <span class="hljs-comment">//从反编译可以看出 values方法，返回 Season2[]</span><br>        <span class="hljs-comment">//含有定义的所有枚举对象</span><br>        Season2[] values = Season2.values();<br>        System.out.println(<span class="hljs-string">&quot;===遍历取出枚举对象(增强for)====&quot;</span>);<br>        <span class="hljs-keyword">for</span> (Season2 season: values) &#123;<span class="hljs-comment">//增强for循环</span><br>            System.out.println(season);<br>        &#125;<br><br>        <span class="hljs-comment">//valueOf：将字符串转换成枚举对象，要求字符串必须为已有的常量名，否则报异常</span><br>        <span class="hljs-comment">//执行流程</span><br>        <span class="hljs-comment">//1. 根据你输入的 &quot;AUTUMN&quot; 到 Season2的枚举对象去查找</span><br>        <span class="hljs-comment">//2. 如果找到了，就返回，如果没有找到，就报错</span><br>        <span class="hljs-type">Season2</span> <span class="hljs-variable">autumn1</span> <span class="hljs-operator">=</span> Season2.valueOf(<span class="hljs-string">&quot;AUTUMN&quot;</span>);<br>        System.out.println(<span class="hljs-string">&quot;autumn1=&quot;</span> + autumn1);<br>        System.out.println(autumn == autumn1);<br><br>        <span class="hljs-comment">//compareTo：比较两个枚举常量，比较的就是编号</span><br>       <br>        <span class="hljs-comment">//1. 就是把 Season2.AUTUMN 枚举对象的编号 和 Season2.SUMMER枚举对象的编号比较</span><br>        <span class="hljs-comment">//2. 看看结果</span><br>        <span class="hljs-comment">/*</span><br><span class="hljs-comment">        public final int compareTo(E o) &#123;</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">            return self.ordinal - other.ordinal;</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">        Season2.AUTUMN的编号[2] - Season2.SUMMER的编号[3]</span><br><span class="hljs-comment">         */</span><br>        System.out.println(Season2.AUTUMN.compareTo(Season2.SUMMER));<br><br>        <span class="hljs-comment">//补充了一个增强for</span><br><span class="hljs-comment">//        int[] nums = &#123;1, 2, 9&#125;;</span><br><span class="hljs-comment">//        //普通的for循环</span><br><span class="hljs-comment">//        System.out.println(&quot;=====普通的for=====&quot;);</span><br><span class="hljs-comment">//        for (int i = 0; i &lt; nums.length; i++) &#123;</span><br><span class="hljs-comment">//            System.out.println(nums[i]);</span><br><span class="hljs-comment">//        &#125;</span><br><span class="hljs-comment">//        System.out.println(&quot;=====增强的for=====&quot;);</span><br><span class="hljs-comment">//        //执行流程是 依次从nums数组中取出数据，赋给i, 如果取出完毕，则退出for</span><br><span class="hljs-comment">//        for(int i : nums) &#123;</span><br><span class="hljs-comment">//            System.out.println(&quot;i=&quot; + i);</span><br><span class="hljs-comment">//        &#125;</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<p><strong>注意点:</strong></p>
<ul>
<li>使用 enum 关键字后，就不能再继承其它类了，因为 enum 会隐式继承 Enum，而 Java 是单继承机制</li>
<li>枚举类和普通类一样，可以实现接口</li>
</ul>
<hr>
<h3 id="方法重写"><a href="#方法重写" class="headerlink" title="方法重写"></a>方法重写</h3><p><strong>注意点</strong></p>
<ol>
<li>子类有一个方法和父类某个方法的名称, 返回类型 ,参数一样</li>
<li>子类的重写方法不能缩小父类方法的访问范围. 比如: public -&gt; privoid</li>
</ol>
<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><h4 id="编译时与运行时"><a href="#编译时与运行时" class="headerlink" title="编译时与运行时"></a>编译时与运行时</h4><ul>
<li>一个对象的编译类型和运行类型可以不一致</li>
<li>编译类型在定义对象时 就确定了 不能改变</li>
<li>运行类型时可以变化的</li>
<li>编译类型看定义时&#x3D;号的左边 , 运行类型看&#x3D;号右边</li>
<li>属性的值看编译类型, 方法执行先后顺序看运行类型</li>
</ul>
<h4 id="向上转型"><a href="#向上转型" class="headerlink" title="向上转型:"></a><strong>向上转型:</strong></h4><p>父类的引用 指向了子类的对象</p>
<h4 id="instance-of"><a href="#instance-of" class="headerlink" title="instance of"></a><strong>instance of</strong></h4><p>判断一个对象的运行类型是否是某一个类型 或者是某一个类型的子类</p>
<h4 id="Java动态绑定机制"><a href="#Java动态绑定机制" class="headerlink" title="Java动态绑定机制"></a>Java动态绑定机制</h4><ol>
<li>当调用对象方法的时候,该方法回合该对象的<strong>内存地址&#x2F;运行类型</strong>绑定</li>
<li>调用对象属性时,没有动态绑定,哪里声明 , 哪里使用</li>
</ol>
<h3 id="package"><a href="#package" class="headerlink" title="package"></a>package</h3><p>用来表示当前类属于哪个包 需要放在类的最上面 最多一个</p>
<figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ada"><span class="hljs-keyword">package</span>  <span class="hljs-title">&lt;包名&gt;</span><br></code></pre></td></tr></table></figure>





<h3 id="断点调试"><a href="#断点调试" class="headerlink" title="断点调试"></a>断点调试</h3><p>断点调试是运行状态,是以对象的运行类型来执行的.</p>
<h3 id="main方法执行说明"><a href="#main方法执行说明" class="headerlink" title="main方法执行说明"></a>main方法执行说明</h3><p><strong>注意点:</strong></p>
<ul>
<li>main方法是java虚拟机调用的</li>
<li>由于虚拟机需要调用 所以方法必须是公用的public</li>
<li>虚拟机在执行是不必创建对象, 方法必须是静态的static</li>
<li>参数是在执行程序时传入的  会形成一个数组</li>
</ul>
<h3 id="代码块的机制"><a href="#代码块的机制" class="headerlink" title="代码块的机制"></a>代码块的机制</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">A</span> &#123;<br>    <span class="hljs-comment">//代码块</span><br>    &#123;<br>        .....<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>代码块调用的顺序要优先于类构造器.  对构造器的补充机制.</li>
</ul>
<h3 id="final"><a href="#final" class="headerlink" title="final"></a>final</h3><ul>
<li>声明类的静态属性  在使用该属性时不会造成类的加载.</li>
</ul>
<h3 id="abstract"><a href="#abstract" class="headerlink" title="abstract"></a>abstract</h3><p><strong>抽象类</strong></p>
<ul>
<li>abstract只能修饰类的方法,不能修饰属性和其他</li>
<li>一个类继承了抽象类,<strong>必须实现父级所有抽象方法</strong>,除非子类本身也是抽象类.</li>
<li>抽象方法不能使用private,final,static来修饰.因为这些关键字都和重写相违背.</li>
</ul>
<h2 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h2><h3 id="定义"><a href="#定义" class="headerlink" title="定义:"></a>定义:</h3><p>接口就是给出一些没有实现的方法,封装到一起,在某个类要使用的时候,再根据具体情况把这些方法写出来.</p>
<h3 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h3><ul>
<li>接口不能被实例化</li>
<li>接口中所有方法时public方法, 接口中抽象方法可以不用abstract修饰</li>
<li>一个普通类实现接口,就必须将该接口的所有方法都实现.</li>
<li>抽象类实现接口, 可以不用实现接口的方法.</li>
<li>一个类可以同时实现多个接口</li>
<li>接口中的属性只能是<code>public static final</code>  修饰</li>
<li>访问接口属性语法 :  接口名.属性名</li>
<li>接口不能继承其他的类,但是可以继承多个别的接口</li>
<li>接口的修饰符只能是public和默认,</li>
</ul>
<p><strong>接口的语法:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//创建接口</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">UsbInterface</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">start</span><span class="hljs-params">()</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">stop</span><span class="hljs-params">()</span>;<br>&#125;<br><br><span class="hljs-comment">//实现接口</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Camera</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UsbInterface</span>&#123;<span class="hljs-comment">//实现接口,就是把接口方法实现</span><br><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">start</span><span class="hljs-params">()</span> &#123;<br>System.out.println(<span class="hljs-string">&quot;相机开始工作...&quot;</span>);<br>&#125;<br><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">stop</span><span class="hljs-params">()</span> &#123;<br>System.out.println(<span class="hljs-string">&quot;相机停止工作....&quot;</span>);<br>&#125;<br>&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Phone</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UsbInterface</span> &#123;<br><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">start</span><span class="hljs-params">()</span> &#123;<br>System.out.println(<span class="hljs-string">&quot;手机开始工作...&quot;</span>);<br>&#125;<br><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">stop</span><span class="hljs-params">()</span> &#123;<br>System.out.println(<span class="hljs-string">&quot;手机停止工作.....&quot;</span>);<br>&#125;<br>&#125;<br><br><br><span class="hljs-comment">//使用接口</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Computer</span> &#123;<br>    <span class="hljs-comment">//编写一个方法, 计算机工作</span><br>    <span class="hljs-comment">//解读:</span><br>    <span class="hljs-comment">//1. UsbInterface usbInterface 形参是接口类型 UsbInterface</span><br>    <span class="hljs-comment">//2. 看到 接收 实现了 UsbInterface接口的类的对象实例</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">work</span><span class="hljs-params">(UsbInterface usbInterface)</span> &#123;<br>        <span class="hljs-comment">//通过接口，来调用方法</span><br>        usbInterface.start();<br>        usbInterface.stop();<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">//在主方法中使用</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">interface01</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-comment">//相机</span><br>        <span class="hljs-type">Camera</span> <span class="hljs-variable">camera</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Camera</span>()<br>        <span class="hljs-comment">//手机</span><br>        <span class="hljs-type">Phone</span> <span class="hljs-variable">phone</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Phone</span>()<br>        <br>        <span class="hljs-comment">//使用</span><br>        <span class="hljs-type">Computer</span> <span class="hljs-variable">computer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Computer</span>()<br>        <br>        computer.work(camera) <span class="hljs-comment">// 输出:相机开始工作 相机结束工作</span><br>        computer.work(phone) <span class="hljs-comment">// 输出:手机开始工作 手机结束工作</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>





<h3 id="接口与继承的关系"><a href="#接口与继承的关系" class="headerlink" title="接口与继承的关系"></a>接口与继承的关系</h3><p>接口是对类继承的补充</p>
<p>(可以一次性实现多个接口)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Father</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">A</span>,B &#123;<br>    <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">Afunc</span><span class="hljs-params">()</span> &#123;&#125;<br>    <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">Bfunc</span><span class="hljs-params">()</span> &#123;&#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="接口的多态"><a href="#接口的多态" class="headerlink" title="接口的多态"></a>接口的多态</h3><ol>
<li><p>接口引用可以指向实现了接口的类的对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">A</span> &#123;&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">B</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">A</span> &#123;<br>    <br>&#125;<br><br><span class="hljs-type">A</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">B</span>() <span class="hljs-comment">//接口A的引用 指向B的实例对象</span><br></code></pre></td></tr></table></figure></li>
</ol>
<h2 id="注解"><a href="#注解" class="headerlink" title="注解"></a>注解</h2><p><strong>语法:</strong></p>
<p>​	使用 <strong>Annotation</strong>(注解) 时要在其前面增加 @ 符号, 并把该 <strong>Annotation</strong> 当成一个修饰符使用。用于修饰它支持的程序元素</p>
<p><strong>三个基本注解:</strong></p>
<ol>
<li>@Override: 限定某个方法，是重写父类方法, 该注解只能用于方法<ul>
<li>可以触发编译器语法校验 , 判断该方法是否真正被重写. 如果不是 触发报错.</li>
<li>只能修饰方法</li>
</ul>
</li>
<li>@Deprecated: 用于表示某个程序元素(类, 方法等)已过时</li>
<li>@SuppressWarnings: 抑制编译器警告</li>
</ol>
<h2 id="java的异常"><a href="#java的异常" class="headerlink" title="java的异常"></a>java的异常</h2><h3 id="运行时异常"><a href="#运行时异常" class="headerlink" title="运行时异常"></a>运行时异常</h3><p><strong>常见 的运行时异常:</strong></p>
<ul>
<li>NullPointerException 空指针异常<ul>
<li>当应用程序试图在需要对象的地方使用 null 时，抛出该异常</li>
</ul>
</li>
<li>ArithmeticException 数学运算异常<ul>
<li>当出现异常的运算条件时，抛出此异常。例如，一个整数“除以零”时，抛出此类的一个实例</li>
</ul>
</li>
<li>ArrayIndexOutOfBoundsException 数组下标越界异常<ul>
<li>用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小，则该索引为非法索引</li>
</ul>
</li>
<li>ClassCastException 类型转换异常<ul>
<li>当试图将对象强制转换为不是实例的子类时，抛出该异常</li>
</ul>
</li>
<li>NumberFormatException 数字格式不正确异常<ul>
<li>当应用程序试图将字符串转换成一种数值类型，但该字符串不能转换为适当格式时，抛出该异常 &#x3D;&gt; 使用异常我们可以确保输入是满足条件数字.</li>
</ul>
</li>
</ul>
<h3 id="编译异常"><a href="#编译异常" class="headerlink" title="编译异常"></a>编译异常</h3><p>编译过程中出现的异常, 不解决无法通过编译.</p>
<p><strong>常见编译异常:</strong></p>
<ul>
<li>SQLException 操作数据库时，查询表可能发生异常</li>
<li>IOException 操作文件时，发生的异常</li>
<li>FileNotFoundException 当操作一个不存在的文件时，发生异常</li>
<li>ClassNotFoundException 加载类，而该类不存在时，异常</li>
<li>EOFException 操作文件，到文件末尾，发生异常</li>
<li>IllegalArguementException 参数异常</li>
</ul>
<h3 id="throw异常处理"><a href="#throw异常处理" class="headerlink" title="throw异常处理"></a>throw异常处理</h3><ol>
<li>如果一个方法(中的语句执行时)可能生成某种异常， 但是并不能确定如何处理这种异常，则此方法应显示地声明抛出异常，表明该方法将不对这些异常进行处理，而由该方法的调用者负责处理。</li>
<li>在方法声明中用throws语句可以声明抛出异常的列表，throws后面的异常类型可以是方法中产生的异常类型，也可以是它的父类。</li>
</ol>
<p><strong>注意点:</strong></p>
<ul>
<li>对于编译异常，程序中必须处理，比如try-catch或者throws</li>
<li>对于运行时异常，程序中如果没有处理，默认就是throws的方式处理</li>
<li>子类重写父类的方法时，对抛出异常的规定:子类重写的方法，所抛出的异常类型要么和父类抛出的异常一致，要么为父类抛出的异常的类型的子类型</li>
<li>在throws过程中，如果有方法try-catch ,就相当于处理异常，就可以不必throws</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ThrowsDetail</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        f2();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">f2</span><span class="hljs-params">()</span> <span class="hljs-comment">/*throws ArithmeticException*/</span> &#123;<br>        <span class="hljs-comment">//1.对于编译异常，程序中必须处理，比如 try-catch 或者 throws</span><br>        <span class="hljs-comment">//2.对于运行时异常，程序中如果没有处理，默认就是throws的方式处理</span><br><br>        <span class="hljs-type">int</span> <span class="hljs-variable">n1</span> <span class="hljs-operator">=</span> <span class="hljs-number">10</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">n2</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-type">double</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> n1 / n2;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">f1</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> FileNotFoundException &#123;<br>        <span class="hljs-comment">//这里大家思考问题 调用f3() 报错</span><br>        <br>        <span class="hljs-comment">//1. 因为f3() 方法抛出的是一个编译异常</span><br>        <span class="hljs-comment">//2. 即这时，就要f1() 必须处理这个编译异常</span><br>        <span class="hljs-comment">//3. 在f1() 中，要么 try-catch-finally ,或者继续throws 这个编译异常</span><br>        f3(); <span class="hljs-comment">// 抛出异常</span><br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">f3</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> FileNotFoundException &#123;<br>        <span class="hljs-type">FileInputStream</span> <span class="hljs-variable">fis</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileInputStream</span>(<span class="hljs-string">&quot;d://aa.txt&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">f4</span><span class="hljs-params">()</span> &#123;<br>        <br>        <span class="hljs-comment">//1. 在f4()中调用方法f5() 是OK</span><br>        <span class="hljs-comment">//2. 原因是f5() 抛出的是运行异常</span><br>        <span class="hljs-comment">//3. 而java中，并不要求程序员显示处理,因为有默认处理机制</span><br>        f5();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">f5</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> ArithmeticException &#123;<br><br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Father</span> &#123; <span class="hljs-comment">//父类</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">method</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> RuntimeException &#123;<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Father</span> &#123;<span class="hljs-comment">//子类</span><br>    <span class="hljs-comment">//3. 子类重写父类的方法时，对抛出异常的规定:子类重写的方法，</span><br>    <span class="hljs-comment">//   所抛出的异常类型要么和父类抛出的异常一致，要么为父类抛出的异常类型的子类型</span><br>    <span class="hljs-comment">//4. 在throws 过程中，如果有方法 try-catch , 就相当于处理异常，就可以不必throws</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">method</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> ArithmeticException &#123;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="自定义异常"><a href="#自定义异常" class="headerlink" title="自定义异常"></a>自定义异常</h3><p><strong>定义:</strong></p>
<p>当程序中出现了某些”错误”，但该错误信息并没有在Throwable子类中描述处理，这个时候可以自己设计异常类，用于描述该错误信息。</p>
<p><strong>步骤:</strong></p>
<ol>
<li>定义类: 自定义异常类名 继承Exception或者RuntimeException</li>
<li>继承exception的为编译异常</li>
<li>继承RuntimeException, 属于运行异常</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CustomException</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-comment">/*throws AgeException*/</span> &#123;<br><br>        <span class="hljs-type">int</span> <span class="hljs-variable">age</span> <span class="hljs-operator">=</span> <span class="hljs-number">180</span>;<br>        <span class="hljs-comment">//要求范围在 18 – 120 之间，否则抛出一个自定义异常</span><br>        <span class="hljs-keyword">if</span>(!(age &gt;= <span class="hljs-number">18</span> &amp;&amp; age &lt;= <span class="hljs-number">120</span>)) &#123;<br>            <span class="hljs-comment">//这里我们可以通过构造器，设置信息</span><br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AgeException</span>(<span class="hljs-string">&quot;年龄需要在 18~120之间&quot;</span>);<br>        &#125;<br>        System.out.println(<span class="hljs-string">&quot;你的年龄范围正确.&quot;</span>);<br>    &#125;<br>&#125;<br><span class="hljs-comment">//自定义一个异常</span><br><br><span class="hljs-comment">//1. 一般情况下，我们自定义异常是继承 RuntimeException</span><br><span class="hljs-comment">//2. 即把自定义异常做成 运行时异常，好处时，我们可以使用默认的处理机制</span><br><span class="hljs-comment">//3. 即比较方便</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">AgeException</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">RuntimeException</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">AgeException</span><span class="hljs-params">(String message)</span> &#123;<span class="hljs-comment">//构造器</span><br>        <span class="hljs-built_in">super</span>(message);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>





<h3 id="throw-和-throws的区别"><a href="#throw-和-throws的区别" class="headerlink" title="throw 和 throws的区别"></a>throw 和 throws的区别</h3><p><strong>throws</strong></p>
<ul>
<li>异常处理的一种方式</li>
<li>写在方法声明处, 后面跟异常类型</li>
</ul>
<p><strong>throw</strong></p>
<ul>
<li>手动生成异常对象的关键字</li>
<li>写在方法体中 , 后面跟需要抛出的异常对象</li>
</ul>
<hr>
<h2 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h2><h3 id="集合的好处"><a href="#集合的好处" class="headerlink" title="集合的好处"></a>集合的好处</h3><ul>
<li>可以动态保存任意多个对象 , 使用比较方便</li>
<li>提供了一系列方便的操作对象方法<ul>
<li>add, remove , set , get</li>
</ul>
</li>
</ul>
<h3 id="集合的分类"><a href="#集合的分类" class="headerlink" title="集合的分类"></a>集合的分类</h3><ul>
<li><p>Collection接口(集合)</p>
<ul>
<li><p>list  List接口扩展自Collection,它可以定义一个允许重复的有序集合，用数组存储元素，可以插入多个null元素，同时它增加了面向位置的操作，允许在指定位置上操作元素，也就是建立了索引，可以用for进行循环遍历。</p>
<ul>
<li>list的实现类常用  ArrayList、LinkedList、Vector；</li>
</ul>
</li>
<li><p>set</p>
<ul>
<li>Set接口扩展子Collection，它是不允许存储重复元素的无序集合，只允许一个null元素，不具备索引，因此不可以用普通for循环遍历（可以用强for循环和迭代器）底层数据结构采用的是哈希表</li>
<li>常用实现类 :  TreeSet、HashSet、LinkedHashSet；</li>
</ul>
</li>
<li><p>queue</p>
</li>
</ul>
</li>
<li><p>Map  键值映射表</p>
<ul>
<li>hashMap</li>
</ul>
</li>
</ul>
<h3 id="ArrayList的扩容机制"><a href="#ArrayList的扩容机制" class="headerlink" title="ArrayList的扩容机制"></a>ArrayList的扩容机制</h3><p>当元素个数达到了容量的75% 时， 新数组的容量通常是原来数组的1.5 倍</p>
<h3 id="List集合的三种遍历方式"><a href="#List集合的三种遍历方式" class="headerlink" title="List集合的三种遍历方式"></a>List集合的三种遍历方式</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//方式一: 使用迭代器 iterator遍历</span><br><span class="hljs-type">Iterator</span> <span class="hljs-variable">iter</span> <span class="hljs-operator">=</span> col.itertator();<br><span class="hljs-keyword">while</span>(iter.hasNext()) &#123;<br>    <span class="hljs-type">Object</span> <span class="hljs-variable">o</span> <span class="hljs-operator">=</span> iter.Next();<br>&#125;<br><br><span class="hljs-comment">//方式二: 使用增强for循环遍历</span><br><span class="hljs-keyword">for</span>(Object o:col) &#123;<br>    <span class="hljs-comment">//o变量就是列表中的每一项</span><br>&#125;<br><br><span class="hljs-comment">//方式三: 普通for循环遍历</span><br><span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i&lt;list.size();i++)&#123;<br>    <span class="hljs-comment">//获取每一个list元素</span><br>    <span class="hljs-type">Object</span> <span class="hljs-variable">object</span> <span class="hljs-operator">=</span> list.get(i);<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="ArrayList和LinkList的选择"><a href="#ArrayList和LinkList的选择" class="headerlink" title="ArrayList和LinkList的选择"></a>ArrayList和LinkList的选择</h4><ul>
<li>查改操作多用ArrayList</li>
<li>增删操作多用LinkList</li>
</ul>
<h3 id="HashSet类型去重"><a href="#HashSet类型去重" class="headerlink" title="HashSet类型去重"></a>HashSet类型去重</h3><p><strong>hashset底层机制:</strong></p>
<ul>
<li>先调用hashcode方法 获取哈希值</li>
<li>对哈希值运算,的到一个索引值, 作为储存位置</li>
<li>如果位置上没有元素 直接存放,  如果有元素 调用equals方法进行判断</li>
</ul>
<p><strong>重写equals, hashcode方法 set&lt;实体&gt;去重:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String name, Integer age)</span>&#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.age = age;<br>    &#125;<br><br>    <span class="hljs-comment">/** 姓名 **/</span><br>    <span class="hljs-keyword">private</span> String name;<br><br>    <span class="hljs-comment">/** 年龄 **/</span><br>    <span class="hljs-keyword">private</span> Integer age;<br><br>    省略get、set方法...<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 重写equals方法，如果对象类型是User,先比较hashcode，一致的场合再比较每个属性的值</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">equals</span><span class="hljs-params">(Object obj)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调用equals方法，当前的hashCode为：&quot;</span>+hashCode());<br>        <span class="hljs-comment">/** 对象是 null 直接返回 false **/</span><br>        <span class="hljs-keyword">if</span> (obj == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>        &#125;<br>        <span class="hljs-comment">/** 对象是当前对象，直接返回 true **/</span><br>        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span> == obj) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125;<br>        <span class="hljs-comment">/** 判断对象类型是否是User **/</span><br>        <span class="hljs-keyword">if</span> (obj <span class="hljs-keyword">instanceof</span> User) &#123;<br>            <span class="hljs-type">User</span> <span class="hljs-variable">vo</span> <span class="hljs-operator">=</span> (User) obj;<br>            <span class="hljs-comment">/** 比较每个属性的值一致时才返回true **/</span><br>            <span class="hljs-comment">/** 有几个对象就要比较几个属性 **/</span><br>            <span class="hljs-keyword">if</span> (vo.name.equals(<span class="hljs-built_in">this</span>.name) &amp;&amp; vo.age.equals(<span class="hljs-built_in">this</span>.age)) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 重写hashcode方法，返回的hashCode一样才再去比较每个属性的值</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">hashCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.getName().hashCode() * 		<span class="hljs-built_in">this</span>.getAge().hashCode();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<hr>
<h3 id="Map"><a href="#Map" class="headerlink" title="Map"></a>Map</h3><p><strong>map的遍历:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Map</span> <span class="hljs-variable">map</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>();<br><span class="hljs-comment">//方案一: 先取出 所有的 Key , 通过 Key 取出对应的 Value</span><br><br><span class="hljs-type">Set</span> <span class="hljs-variable">keyset</span> <span class="hljs-operator">=</span> map.keySet();<br><span class="hljs-comment">//1-1 增强for循环</span><br><br><span class="hljs-keyword">for</span> (Object key : keyset) &#123;<br>System.out.println(key + <span class="hljs-string">&quot;-&quot;</span> + map.get(key));<br>&#125;<br><span class="hljs-comment">//1-2 迭代器</span><br><br><span class="hljs-type">Iterator</span> <span class="hljs-variable">iterator</span> <span class="hljs-operator">=</span> keyset.iterator();<br><span class="hljs-keyword">while</span> (iterator.hasNext()) &#123;<br><span class="hljs-type">Object</span> <span class="hljs-variable">key</span> <span class="hljs-operator">=</span> iterator.next();<br>System.out.println(key + <span class="hljs-string">&quot;-&quot;</span> + map.get(key));<br>&#125;<br><br><span class="hljs-comment">//方案二: 把所有的value取出</span><br><span class="hljs-type">Collection</span> <span class="hljs-variable">values</span> <span class="hljs-operator">=</span> map.values();<br><br><span class="hljs-keyword">for</span> (Object value : values) &#123;<br>System.out.println(value);<br>&#125;<br><span class="hljs-comment">//迭代器方法...</span><br><br><span class="hljs-comment">//方案三: 通过 EntrySet 来获取 k-v</span><br><span class="hljs-type">Set</span> <span class="hljs-variable">entrySet</span> <span class="hljs-operator">=</span> map.entrySet();<br><br><span class="hljs-keyword">for</span> (Object entry : entrySet) &#123;<br><span class="hljs-comment">//将 entry 转成 Map.Entry</span><br>Map.<span class="hljs-type">Entry</span> <span class="hljs-variable">m</span> <span class="hljs-operator">=</span> (Map.Entry) entry;<br>System.out.println(m.getKey() + <span class="hljs-string">&quot;-&quot;</span> + m.getValue());<br>&#125;<br><br><span class="hljs-comment">//迭代器</span><br><span class="hljs-type">Iterator</span> <span class="hljs-variable">iterator3</span> <span class="hljs-operator">=</span> entrySet.iterator();<br><span class="hljs-keyword">while</span> (iterator3.hasNext()) &#123;<br><span class="hljs-type">Object</span> <span class="hljs-variable">entry</span> <span class="hljs-operator">=</span> iterator3.next();<br><br><span class="hljs-comment">//向下转型 Map.Entry</span><br>Map.<span class="hljs-type">Entry</span> <span class="hljs-variable">m</span> <span class="hljs-operator">=</span> (Map.Entry) entry;<br>System.out.println(m.getKey() + <span class="hljs-string">&quot;-&quot;</span> + m.getValue());<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="HashMap"><a href="#HashMap" class="headerlink" title="HashMap"></a>HashMap</h4><p><strong>添加元素的机制:</strong></p>
<ul>
<li>当添加key-val时，通过key的哈希值得到在table的索引。然后判断该索引处是否有元素,如果没有元素直接添加。如果该索引处有元素,继续判断该元素的key和准备加入的key相是否等，如果相等，则直接替换val;如果不相等需要判断是树结构还是链表结构，做出相应处理。如果添加时发现容量不够，则需要扩容。</li>
</ul>
<h4 id="HashTable"><a href="#HashTable" class="headerlink" title="HashTable"></a>HashTable</h4><p><strong>注意点:</strong></p>
<ul>
<li>HashTable的键和值都不能为null,否则报错</li>
<li>hashtable线程安全</li>
</ul>
<h4 id="Properties"><a href="#Properties" class="headerlink" title="Properties"></a>Properties</h4><p><strong>特点:</strong></p>
<ul>
<li>可以从.Properties 文件中读取数据到 Properties 类对象中, 还可以修改.</li>
</ul>
<h3 id="开发中集合的选择"><a href="#开发中集合的选择" class="headerlink" title="开发中集合的选择"></a>开发中集合的选择</h3><ol>
<li>先判断存储的类型(一组对象[单列]或一组键值对[双列)</li>
<li>一组对象[单列]: Collection接口<ul>
<li>允许重复: List<ul>
<li>增删多: LinkedList (底层维护了一个双向链表)</li>
<li>改查多: ArrayList (底层维护Object类型的可变数组)</li>
</ul>
</li>
<li>不允许重复: set<ul>
<li>无序 : HashSet (底层是HashMap，维护了一个哈希表即(数组+链表+红黑树)</li>
<li>排序: TreeSet</li>
<li>插入和取出顺序一致: linkedHashset , 维护数组加双向链表</li>
</ul>
</li>
</ul>
</li>
<li>键值对 k-v<ul>
<li>键无序: HashMap [底层是:哈希表jdk7:数组+链表,jdk8:数组+链表+红黑树]</li>
<li>键排序: TreeMap</li>
<li>键无序: HashMap [底层是:哈希表jdk7:数组+链表,jdk8:数组+链表+红黑树]</li>
<li>读取文件: Properties</li>
</ul>
</li>
</ol>
<hr>
<h2 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h2><p>可以去接收数据类型的一种数据类型</p>
<hr>
<h2 id="多线程"><a href="#多线程" class="headerlink" title="多线程"></a>多线程</h2><p>一个进程 多个线程</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//创建一个线程 需实例化一个Thread实力 并复写run方法</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Thread</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyThread</span>();<br>        t.start(); <span class="hljs-comment">// 启动新线程</span><br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Thread</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;start new thread!&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="线程的几种状态"><a href="#线程的几种状态" class="headerlink" title="线程的几种状态"></a>线程的几种状态</h3><ul>
<li>New：新创建的线程，尚未执行；</li>
<li>Runnable：运行中的线程，正在执行<code>run()</code>方法的Java代码；</li>
<li>Blocked：运行中的线程，因为某些操作被阻塞而挂起；</li>
<li>Waiting：运行中的线程，因为某些操作在等待中；</li>
<li>Timed Waiting：运行中的线程，因为执行<code>sleep()</code>方法正在计时等待；</li>
<li>Terminated：线程已终止，因为<code>run()</code>方法执行完毕。</li>
</ul>
<h4 id="线程终止的原因"><a href="#线程终止的原因" class="headerlink" title="线程终止的原因"></a>线程终止的原因</h4><ul>
<li>正常终止: <code>run()</code>方法执行完毕 </li>
<li>意外终止: <code>run()</code>方法执行过程中遇到异常 导致线程终止</li>
<li>对thread实力调用 <code>stop()</code>方法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//等待线程完成再运行</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">Thread</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(() -&gt; &#123;<br>            System.out.println(<span class="hljs-string">&quot;hello&quot;</span>);<br>        &#125;);<br>        System.out.println(<span class="hljs-string">&quot;start&quot;</span>);<br>        t.start();<br>        <span class="hljs-comment">//等待t线程运行完毕再运行main主线程</span><br>        t.join();<br>        System.out.println(<span class="hljs-string">&quot;end&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="中断线程"><a href="#中断线程" class="headerlink" title="中断线程"></a>中断线程</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">Thread</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyThread</span>();<br>        t.start();<br>        Thread.sleep(<span class="hljs-number">1</span>); <span class="hljs-comment">// 暂停1毫秒</span><br>        t.interrupt(); <span class="hljs-comment">// 中断t线程 发送一个中断请求,需要目标线程有响应</span><br>        <span class="hljs-comment">//如果再join等待时 另外的线程向main线程发出中断请求 main会立马结束</span><br>        t.join(); <span class="hljs-comment">// 等待t线程结束</span><br>        System.out.println(<span class="hljs-string">&quot;end&quot;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Thread</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-comment">//如果没有终端请求就继续循环</span><br>        <span class="hljs-keyword">while</span> (! isInterrupted()) &#123;<br>            n ++;<br>            System.out.println(n + <span class="hljs-string">&quot; hello!&quot;</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<p><strong>利用标志位中断</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span>  <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">HelloThread</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HelloThread</span>();<br>        t.start();<br>        Thread.sleep(<span class="hljs-number">1</span>);<br>        t.running = <span class="hljs-literal">false</span>; <span class="hljs-comment">// 标志位置为false</span><br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Thread</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">volatile</span> <span class="hljs-type">boolean</span> <span class="hljs-variable">running</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">while</span> (running) &#123;<br>            n ++;<br>            System.out.println(n + <span class="hljs-string">&quot; hello!&quot;</span>);<br>        &#125;<br>        System.out.println(<span class="hljs-string">&quot;end!&quot;</span>);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p><strong>为什么标志符要用 volatile声明:</strong></p>
<p>在Java虚拟机中，变量的值保存在主内存中，但是，当线程访问变量时，它会先获取一个副本，并保存在自己的工作内存中。如果线程修改了变量的值，虚拟机会在某个时刻把修改后的值回写到主内存，但是，这个时间是<strong>不确定的</strong>.  </p>
<p>导致多个线程读取变量有滞后性,  某个线程读取的可能是未更新之前的标识符变量.</p>
<p><strong>volatile告诉虚拟机</strong></p>
<ul>
<li>每次访问变量时，总是获取主内存的最新值；</li>
<li>每次修改变量后，立刻回写到主内存。</li>
</ul>
<h3 id="守护线程"><a href="#守护线程" class="headerlink" title="守护线程"></a>守护线程</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Thread</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyThread</span>();<br>t.setDaemon(<span class="hljs-literal">true</span>); <span class="hljs-comment">//添加为守护线程</span><br>t.start();<br></code></pre></td></tr></table></figure>

<p>所有非守护线程结束后,无论有没有守护线程, 虚拟机都会自动退出.</p>
<p><strong>注意:</strong></p>
<ul>
<li>守护线程不能持有任何需要关闭的资源，例如打开文件等，因为虚拟机退出时，守护线程没有任何机会来关闭文件，这会导致数据丢失</li>
</ul>
<h3 id="线程同步"><a href="#线程同步" class="headerlink" title="线程同步"></a>线程同步</h3><p>当多个线程操作(<strong>非原子操作</strong>)一个公共变量时,会出现数据不一致的情况. 此时需要用到锁</p>
<blockquote>
<p>非原子操作会被操作系统拆分成几步完成</p>
</blockquote>
<h4 id="锁"><a href="#锁" class="headerlink" title="锁"></a>锁</h4><p>synchronized关键字</p>
<ul>
<li>保证了代码块在任意时刻最多只有一个线程能执行</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-type">var</span> <span class="hljs-variable">add</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AddThread</span>();<br>        <span class="hljs-type">var</span> <span class="hljs-variable">dec</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DecThread</span>();<br>        add.start();<br>        dec.start();<br>        add.join();<br>        dec.join();<br>        System.out.println(Counter.count);<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Counter</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Object</span> <span class="hljs-variable">lock</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Object</span>();<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">AddThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Thread</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10000</span>; i++) &#123;<br>            <span class="hljs-keyword">synchronized</span>(Counter.lock) &#123;<br>                Counter.count += <span class="hljs-number">1</span>;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">DecThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Thread</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10000</span>; i++) &#123;<br>            <span class="hljs-keyword">synchronized</span>(Counter.lock) &#123;<br>                Counter.count -= <span class="hljs-number">1</span>;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br><span class="hljs-comment">//它表示用Counter.lock实例作为锁，两个线程在执行各自的synchronized(Counter.lock) &#123; ... &#125;代码块时，必须先获得锁，才能进入代码块进行。执行结束后，在`synchronized`语句块结束会自动释放锁。这样一来，对Counter.count变量进行读写就不可能同时进行。上述代码无论运行多少次，最终结果都是0</span><br><br></code></pre></td></tr></table></figure>

<p><strong>注意:</strong></p>
<ul>
<li>在使用synchronized不用担心抛出异常,  无论是否有异常 都会在结束出正确释放锁.</li>
<li>需要同步的一组线程 对应一个锁实例</li>
<li>单条原子操作的语句不需要同步</li>
</ul>
<p><strong>不需要synchronized的操作</strong>  (单条操作语句的情况下)</p>
<ul>
<li>基本类型（<code>long</code>和<code>double</code>除外）赋值，例如：<code>int n = m</code>；</li>
<li>引用类型赋值，例如：<code>List&lt;String&gt; list = anotherList</code></li>
</ul>
<h4 id="同步方法"><a href="#同步方法" class="headerlink" title="同步方法"></a>同步方法</h4><p>封装<strong>synchronized</strong>逻辑</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Counter</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span> &#123;<br>        <span class="hljs-comment">//锁住this 可以再创建多个实例时互不影响</span><br>        <span class="hljs-keyword">synchronized</span>(<span class="hljs-built_in">this</span>) &#123;<br>            count += n;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">dec</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span> &#123;<br>        <span class="hljs-keyword">synchronized</span>(<span class="hljs-built_in">this</span>) &#123;<br>            count -= n;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">get</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> count;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>上述锁住this的写法也等同于以下  直接用synchronized来修饰方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">synchronized</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">dec</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span> &#123;<br>       count -= n;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>当用synchronized修饰静态方法时 他锁住的就是class实例</p>
<hr>
<h2 id="IO文件操作"><a href="#IO文件操作" class="headerlink" title="IO文件操作"></a>IO文件操作</h2><h3 id="利用inputStream-和-outputStream拷贝文件"><a href="#利用inputStream-和-outputStream拷贝文件" class="headerlink" title="利用inputStream 和 outputStream拷贝文件"></a><strong>利用inputStream 和 outputStream拷贝文件</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CopyTest</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">FileInputStream</span> <span class="hljs-variable">fileInput</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>        <span class="hljs-type">FileOutputStream</span> <span class="hljs-variable">fileOutput</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br><br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-type">String</span> <span class="hljs-variable">path</span> <span class="hljs-operator">=</span> System.getProperty(<span class="hljs-string">&quot;user.dir&quot;</span>) + <span class="hljs-string">&quot;\\file\\hello.txt&quot;</span>;<br>            <span class="hljs-type">String</span> <span class="hljs-variable">toPath</span> <span class="hljs-operator">=</span> System.getProperty(<span class="hljs-string">&quot;user.dir&quot;</span>) + <span class="hljs-string">&quot;\\file\\helloCopy.txt&quot;</span>;<br>            fileInput = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileInputStream</span>(path);<br>            fileOutput = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileOutputStream</span>(toPath);<br>            <span class="hljs-type">byte</span>[] byt = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[<span class="hljs-number">1024</span>]; <span class="hljs-comment">//读取的缓存区</span><br><br>            <span class="hljs-type">int</span> <span class="hljs-variable">readLength</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; <span class="hljs-comment">//每次读取的长度</span><br><br>            <span class="hljs-comment">//循环读取</span><br>            <span class="hljs-keyword">while</span> ((readLength = fileInput.read(byt)) != -<span class="hljs-number">1</span>) &#123;<br>                <span class="hljs-comment">//写入</span><br>                fileOutput.write(byt,<span class="hljs-number">0</span>,readLength);<br>            &#125;<br><br><br>            System.out.println(fileInput);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            System.out.println(e + <span class="hljs-string">&quot;error&quot;</span>);<br>        &#125; <span class="hljs-keyword">finally</span> &#123;<br>            <span class="hljs-comment">//关闭流</span><br>            <span class="hljs-keyword">try</span> &#123;<br>                <span class="hljs-keyword">if</span> (fileInput != <span class="hljs-literal">null</span>) &#123;<br>                    fileInput.close();<br>                &#125;<br><br>                <span class="hljs-keyword">if</span> (fileOutput != <span class="hljs-literal">null</span>) &#123;<br>                    fileOutput.close();<br>                &#125;<br>            &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>                System.out.println(e + <span class="hljs-string">&quot;error&quot;</span>);<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="Reader和InputStream有什么关系？"><a href="#Reader和InputStream有什么关系？" class="headerlink" title="Reader和InputStream有什么关系？"></a><code>Reader</code>和<code>InputStream</code>有什么关系？</h3><p>除了特殊的<code>CharArrayReader</code>和<code>StringReader</code>，普通的<code>Reader</code>实际上是基于<code>InputStream</code>构造的，因为<code>Reader</code>需要从<code>InputStream</code>中读入字节流（<code>byte</code>），然后，根据编码设置，再转换为<code>char</code>就可以实现字符流。如果我们查看<code>FileReader</code>的源码，它在内部实际上持有一个<code>FileInputStream</code>。</p>
<h3 id="InputStream转化成Reader"><a href="#InputStream转化成Reader" class="headerlink" title="InputStream转化成Reader"></a><code>InputStream</code>转化成<code>Reader</code></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 持有InputStream:</span><br><span class="hljs-type">InputStream</span> <span class="hljs-variable">input</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileInputStream</span>(<span class="hljs-string">&quot;src/readme.txt&quot;</span>);<br><span class="hljs-comment">// 变换为Reader:</span><br><span class="hljs-type">Reader</span> <span class="hljs-variable">reader</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InputStreamReader</span>(input, <span class="hljs-string">&quot;UTF-8&quot;</span>);<br></code></pre></td></tr></table></figure>



<h3 id="writer"><a href="#writer" class="headerlink" title="writer"></a>writer</h3><ul>
<li><p><code>Writer</code>定义了所有字符输出流的超类：</p>
<ul>
<li><p><code>FileWriter</code>实现了文件字符流输出；</p>
</li>
<li><p><code>CharArrayWriter</code>和<code>StringWriter</code>在内存中模拟一个字符流输出。</p>
</li>
</ul>
</li>
<li><p>使用<code>try (resource)</code>保证<code>Writer</code>正确关闭。</p>
</li>
<li><p><code>Writer</code>是基于<code>OutputStream</code>构造的，可以通过<code>OutputStreamWriter</code>将<code>OutputStream</code>转换为<code>Writer</code>，转换时需要指定编码。</p>
</li>
<li><p>new FileWriter(file&#x2F;String)  覆盖模式, 流的指针在首端</p>
</li>
<li><p>new FileWriter(file&#x2F;String , true)  追加模式, 流的指针在尾端</p>
</li>
</ul>
<hr>
<h2 id="反射"><a href="#反射" class="headerlink" title="反射"></a>反射</h2><p><strong>注意点</strong></p>
<ul>
<li>反射无法读取私有属性</li>
<li>反射调用方法比正常调用所占资源要多</li>
<li>关闭访问检查可以优化反射调用方法的时间<ul>
<li>setAccessible(true)</li>
</ul>
</li>
</ul>
<hr>
<h3 id="class类"><a href="#class类" class="headerlink" title="class类"></a>class类</h3><ul>
<li>class类也继承object</li>
<li>class类是由系统创建</li>
<li>某个类的class对象 在内存中只有一份</li>
<li>class对象事存放在堆中的</li>
<li>类的字节码二进制数据 是放在方法区的</li>
</ul>
<h4 id="类的加载时机"><a href="#类的加载时机" class="headerlink" title="类的加载时机"></a>类的加载时机</h4><ul>
<li>创建对象时</li>
<li>子类被加载时, 父类也加载</li>
<li>调用类中的静态成员</li>
<li>通过反射动态加载</li>
</ul>
<h4 id="类的加载过程"><a href="#类的加载过程" class="headerlink" title="类的加载过程"></a>类的加载过程</h4><ol>
<li>java源码通过javac编译成字节码文件(xxx.class)</li>
<li>将类的class文件 ( jar包 ) 读入内存 并创建一个class对象(由类的加载器完成)</li>
<li>将类的二进制数据合并到<code>JRE</code>中</li>
<li><code>jvm</code> 负责对类的静态成员进行初始化  (会保证类的同步执行)</li>
</ol>
<h3 id="通过反射创建类"><a href="#通过反射创建类" class="headerlink" title="通过反射创建类"></a>通过反射创建类</h3><p><strong>前提</strong></p>
<ul>
<li>这个类必须有public无参构造器</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CreateClass</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException &#123;<br>        <span class="hljs-comment">//利用反射创建类</span><br><br>        <span class="hljs-comment">//获取该类的class类</span><br>        <span class="hljs-type">Class</span> <span class="hljs-variable">cls</span> <span class="hljs-operator">=</span> Class.forName(<span class="hljs-string">&quot;com.Dzx.Reflect.CreateClass.Cat&quot;</span>);<br><br>        <span class="hljs-comment">//通过无参构造器创建</span><br>        <span class="hljs-type">Cat</span> <span class="hljs-variable">cat1</span> <span class="hljs-operator">=</span> (Cat) cls.newInstance();<br><br>        <span class="hljs-comment">//使用有参构造器创造</span><br>        <span class="hljs-comment">//1  得到constract构造器对象</span><br>        Constructor&lt;?&gt; constructor = cls.getConstructor();<br>        <span class="hljs-type">Cat</span> <span class="hljs-variable">cat2</span> <span class="hljs-operator">=</span>(Cat) constructor.newInstance(<span class="hljs-string">&quot;超级老毛&quot;</span>);<br>    &#125;<br>&#125;<br><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Cat</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;邓子雄&quot;</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Cat</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;我是一只猫&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Cat</span><span class="hljs-params">(String name)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;我是一只名叫&quot;</span> + name + <span class="hljs-string">&quot;的猫&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="反射设置属性"><a href="#反射设置属性" class="headerlink" title="反射设置属性"></a>反射设置属性</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SetProperty</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException &#123;<br>        <span class="hljs-comment">//获取class</span><br>        <span class="hljs-type">Class</span> <span class="hljs-variable">cls</span> <span class="hljs-operator">=</span> Class.forName(<span class="hljs-string">&quot;com.Dzx.Reflect.CreateClass.NewCat&quot;</span>);<br><br>        <span class="hljs-comment">//获取对象</span><br>        <span class="hljs-type">NewCat</span> <span class="hljs-variable">newCat</span> <span class="hljs-operator">=</span> (NewCat) cls.newInstance();<br><br>        <span class="hljs-comment">//获得属性</span><br>        <span class="hljs-type">Field</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> cls.getField(<span class="hljs-string">&quot;name&quot;</span>);<br>        <span class="hljs-type">String</span> <span class="hljs-variable">str</span> <span class="hljs-operator">=</span> (String) name.get(newCat);<br>        System.out.println(str);<br>        <span class="hljs-comment">//设置属性</span><br>        name.set(newCat,<span class="hljs-string">&quot;超级老毛&quot;</span>);<br>        System.out.println(newCat.name);<br><br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>



<p>反射获取并调用方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">GetMethod</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException &#123;<br>        <span class="hljs-comment">//获取class</span><br>        <span class="hljs-type">Class</span> <span class="hljs-variable">cls</span> <span class="hljs-operator">=</span> Class.forName(<span class="hljs-string">&quot;com.Dzx.Reflect.CreateClass.GetMethod&quot;</span>);<br>        <span class="hljs-comment">//获取对象</span><br>        <span class="hljs-type">Object</span> <span class="hljs-variable">method</span> <span class="hljs-operator">=</span> cls.newInstance();<br>        <span class="hljs-comment">//获取方法</span><br>        java.lang.reflect.<span class="hljs-type">Method</span> <span class="hljs-variable">big</span> <span class="hljs-operator">=</span> cls.getDeclaredMethod(<span class="hljs-string">&quot;big&quot;</span>);<br>        <span class="hljs-comment">//调用方法</span><br>        big.invoke(method);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>







<h2 id="sql使用"><a href="#sql使用" class="headerlink" title="sql使用"></a>sql使用</h2><h3 id="insert插入使用细节"><a href="#insert插入使用细节" class="headerlink" title="insert插入使用细节"></a>insert插入使用细节</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> `表名` (字段名)<br><span class="hljs-keyword">VALUES</span>(对应值);<br></code></pre></td></tr></table></figure>



<ul>
<li>插入的数据应与字段的数据类型相同。</li>
<li>数据的长度应在列的规定范围内，例如：不能将一个长度为 80 的字符串加入到长度为 40 的列中。</li>
<li>在 values 中列出的数据位置必须与被加入的列的排列位置相对应。</li>
<li>字符和日期型数据应包含在单引号中。</li>
<li>列可以插入空值[前提是该字段允许为空]，insert into table value(null)</li>
<li>insert into tab_name (列名..) values (),(),() 形式添加多条记录</li>
<li>如果是给表中的所有字段添加数据，可以不写前面的字段名称</li>
<li>当不给某个字段值时，如果有默认值就会添加默认值，否则报错</li>
</ul>
<h3 id="update-使用细节"><a href="#update-使用细节" class="headerlink" title="update 使用细节"></a>update 使用细节</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">UPDATE</span> 表名<br><span class="hljs-keyword">SET</span> 要修改的字段名 <span class="hljs-operator">=</span> 值<br><span class="hljs-keyword">WHERE</span> 数据的唯一索引 <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;索引值&#x27;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>update语法可以用新值更新原有表行中的各列</li>
<li>set子语句指示要修改哪些列和姚给予哪些值</li>
<li>where 子语句指定应更新哪些行. 如果没有where则更新所有行</li>
<li>修改多个字段<ul>
<li><code>set 字段1 = 值1 , 字段2 = 值2</code></li>
</ul>
</li>
</ul>
<h3 id="delete-使用细节"><a href="#delete-使用细节" class="headerlink" title="delete 使用细节"></a>delete 使用细节</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DELETE</span> <span class="hljs-keyword">FROM</span> 表名<br><span class="hljs-keyword">WHERE</span> 指定删除行的字段名 <span class="hljs-operator">=</span> 值;<br></code></pre></td></tr></table></figure>

<ul>
<li>如果不使用where子句 将删除表中所有数据</li>
<li>delete语句不能删除某一列的值(可使用update设置成null)</li>
<li>使用delete语句仅删除记录,不删除表本身.</li>
</ul>
<h3 id="select使用细节"><a href="#select使用细节" class="headerlink" title="select使用细节"></a>select使用细节</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> [可选的过滤字段] <span class="hljs-operator">*</span> <span class="hljs-operator">|</span> &#123;列名<span class="hljs-number">1</span>,列名<span class="hljs-number">2.</span>..&#125; <span class="hljs-keyword">FROM</span> 表名<br></code></pre></td></tr></table></figure>

<ul>
<li>select可以运算<ul>
<li>SELECT <code>name</code>, (chinese+english+math) FROM student;</li>
</ul>
</li>
</ul>
<h4 id="count函数"><a href="#count函数" class="headerlink" title="count函数"></a>count函数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span>查询student表 数学成绩大于<span class="hljs-number">90</span>分的人数<br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>) <span class="hljs-keyword">FROM</span> student<br><span class="hljs-keyword">WHERE</span> math <span class="hljs-operator">&gt;</span> <span class="hljs-number">90</span><br></code></pre></td></tr></table></figure>

<p><strong>count(*)和count(列)的区别:</strong></p>
<ul>
<li>count(*) 返回满足条件的记录的行数</li>
<li>count(列): 统计满足条件的某列有多少个，但是会排除 为 null 的情况</li>
</ul>
<h4 id="sum函数"><a href="#sum函数" class="headerlink" title="sum函数"></a>sum函数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">-- 统计一个班级数学总成绩？</span><br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(math) <span class="hljs-keyword">FROM</span> student; <br><span class="hljs-comment">-- 统计一个班级语文、英语、数学各科的总成绩</span><br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(math) <span class="hljs-keyword">AS</span> math_total_score,<span class="hljs-built_in">SUM</span>(english),<span class="hljs-built_in">SUM</span>(chinese) <span class="hljs-keyword">FROM</span> student;<br></code></pre></td></tr></table></figure>



<h4 id="AVG函数"><a href="#AVG函数" class="headerlink" title="AVG函数"></a>AVG函数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">-- 求一个班级数学平均分？</span><br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">AVG</span>(math) <span class="hljs-keyword">FROM</span> student; <br><span class="hljs-comment">-- 求一个班级总分平均分</span><br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">AVG</span>(math <span class="hljs-operator">+</span> english <span class="hljs-operator">+</span> chinese) <span class="hljs-keyword">FROM</span> student;<br></code></pre></td></tr></table></figure>



<h4 id="max和min函数"><a href="#max和min函数" class="headerlink" title="max和min函数"></a>max和min函数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">-- 求班级最高分和最低分（数值范围在统计中特别有用）</span><br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">MAX</span>(math <span class="hljs-operator">+</span> english <span class="hljs-operator">+</span> chinese), <span class="hljs-built_in">MIN</span>(math <span class="hljs-operator">+</span> english <span class="hljs-operator">+</span> chinese)<br><span class="hljs-keyword">FROM</span> student;<br></code></pre></td></tr></table></figure>







<h3 id="where语句中常用运算符"><a href="#where语句中常用运算符" class="headerlink" title="where语句中常用运算符"></a>where语句中常用运算符</h3><h4 id="比较运算符"><a href="#比较运算符" class="headerlink" title="比较运算符"></a>比较运算符</h4><ul>
<li><code>BETWEEN ... AND ...</code>  显示在某一区间的值</li>
<li><code>IN(set)</code>  显示在in列表中的值  例:in(100,200)</li>
<li><code>LIKE</code>   模糊查询</li>
<li><code>NOT LIKE </code>   不模糊查询</li>
<li><code>IS NULL</code>   判断是否为空</li>
</ul>
<h4 id="逻辑运算符-1"><a href="#逻辑运算符-1" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><ul>
<li><code>AND</code> 多个条件同时成立</li>
<li><code>or</code> 多个条件任一成立</li>
<li><code>not</code> 不成立</li>
</ul>
<h3 id="group-by-语句"><a href="#group-by-语句" class="headerlink" title="group by 语句"></a>group by 语句</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span>对查询结果进行分组统计<br><span class="hljs-keyword">SELECT</span> column1,column2,column3.... <span class="hljs-keyword">FROM</span> table_name<br><span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> column3... <span class="hljs-keyword">HAVING</span> ....<br></code></pre></td></tr></table></figure>





<h3 id="字符串相关函数"><a href="#字符串相关函数" class="headerlink" title="字符串相关函数"></a>字符串相关函数</h3><ul>
<li><code>CHARSET(str)</code>   返回字串字符集</li>
<li><code>CONCAT(string2[....])</code>    连接字串</li>
<li><code>INSTR(string, substring)</code>   返回<code>substring</code>在<code>string</code>中出现的位置, 没有就返回0</li>
<li><code>UCASE(string2)</code>   转换成大写</li>
<li><code>LCASE(string2)</code>  转换成小写</li>
<li><code>LEFT(string2,length)</code>   从string2中的左边起取length个字符</li>
<li><code>LENGTH(string)</code>         string长度[按照字节]</li>
<li><code>REPLACE(str, serarch_str,replace_str)</code>   在str中用replace_str替换search_str</li>
<li><code>STRCMP(string1,string2)</code>     逐字比较两字符串大小</li>
<li><code>SUBSTRING(str,position[,length])</code>  从str的position开始  取length个字符</li>
<li><code>LTRIM(string2)  RTRIM(string2)</code>   去除前端空格或者后端空格</li>
</ul>
<h3 id="数学相关函数"><a href="#数学相关函数" class="headerlink" title="数学相关函数"></a>数学相关函数</h3><ul>
<li><code>ABS(num)</code> <ul>
<li>绝对值</li>
</ul>
</li>
<li><code>BIN(number)</code><ul>
<li>十进制转换二进制</li>
</ul>
</li>
<li><code>CEILING(numb)</code><ul>
<li>向上取整  的到比numb大的最小整数</li>
</ul>
</li>
<li><code>CONV(num,from_base,to_base)</code><ul>
<li>进制转换</li>
</ul>
</li>
<li><code>FLOOR(numb)</code><ul>
<li>向下取整,得到比numb小的最大整数</li>
</ul>
</li>
<li><code>FORMAT(numb,decimal_places)</code><ul>
<li>保留小数位数</li>
</ul>
</li>
<li><code>HEX(decimalNumber)</code><ul>
<li>转16进制</li>
</ul>
</li>
<li><code>LEAST(num,num2[,...])</code><ul>
<li>求最小值</li>
</ul>
</li>
<li><code>MOD(num,denominator)</code><ul>
<li>求余</li>
</ul>
</li>
<li><code>RAND([seed])</code><ul>
<li>取随机数  范围是0 &lt;&#x3D; v &lt;&#x3D;1.0</li>
</ul>
</li>
</ul>
<h3 id="流程控制函数"><a href="#流程控制函数" class="headerlink" title="流程控制函数"></a>流程控制函数</h3><ul>
<li><code>IF(expr1,expr2,expr3)</code><ul>
<li>如果expr1为true, 则返回expr2  否则返回expr3</li>
</ul>
</li>
<li><code>IFNULL(expr1,expr2)</code><ul>
<li>如果expr1不为空NULL,则返回expr1, 否则返回expr2</li>
</ul>
</li>
</ul>
<h3 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h3><p><strong>基本语法</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span> <span class="hljs-keyword">start</span> 开始页  size 每页的条数<br><span class="hljs-keyword">SELECT</span> ... <br>LIMIT <span class="hljs-keyword">start</span>,size <br></code></pre></td></tr></table></figure>

<p><strong>分页公式</strong></p>
<p><code>LIMIT</code>  每页显示记录数 * (第几页 - 1) , 每页显示记录数</p>
<h3 id="多表查询"><a href="#多表查询" class="headerlink" title="多表查询"></a>多表查询</h3><p><strong>注意点</strong></p>
<ul>
<li>多表查询的WHERE条件不能少于 表的个数-1, 否则会出现笛卡尔集</li>
</ul>
<h4 id="自连接"><a href="#自连接" class="headerlink" title="自连接"></a>自连接</h4><ul>
<li>把同一张表当做两张表使用</li>
<li>需要给表取别名</li>
<li>列名不明确，可以指定列的别名</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> worker.ename <span class="hljs-keyword">AS</span> <span class="hljs-string">&#x27;职员名&#x27;</span> , boss.ename <span class="hljs-keyword">AS</span> <span class="hljs-string">&#x27;上级名&#x27;</span> <br><span class="hljs-keyword">FROM</span> emp worker, emp boss<br><span class="hljs-keyword">WHERE</span> worker.mgr <span class="hljs-operator">=</span> boss.empno;<br></code></pre></td></tr></table></figure>





<h3 id="合并查询"><a href="#合并查询" class="headerlink" title="合并查询"></a>合并查询</h3><p><strong>UNION ALL:</strong></p>
<p>用于去的两个结果集的并集, 使用该操作符时 不会取消重复行</p>
<p><strong>UNION</strong></p>
<p>与union all 相似 , 但是会去掉重复行</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> ename,sal,job <span class="hljs-keyword">FROM</span> emp <span class="hljs-keyword">WHERE</span> sal<span class="hljs-operator">&gt;</span><span class="hljs-number">2500</span> <span class="hljs-comment">-- 5</span><br><span class="hljs-keyword">UNION</span> <span class="hljs-keyword">ALL</span>  <span class="hljs-comment">--合并查询关键字</span><br><span class="hljs-keyword">SELECT</span> ename,sal,job <span class="hljs-keyword">FROM</span> emp <span class="hljs-keyword">WHERE</span> job<span class="hljs-operator">=</span><span class="hljs-string">&#x27;MANAGER&#x27;</span> <span class="hljs-comment">-- 3</span><br></code></pre></td></tr></table></figure>



<h3 id="外连接"><a href="#外连接" class="headerlink" title="外连接"></a>外连接</h3><h4 id="左外连接"><a href="#左外连接" class="headerlink" title="左外连接"></a>左外连接</h4><p>如果左侧的表完全显示,则是左外连接</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">--左侧表没有满足条件的数据 全部显示</span><br><span class="hljs-comment">--显示所有人的成绩，如果没有成绩，也要显示该人的姓名和 id 号,成绩显示为空</span><br><br><span class="hljs-keyword">SELECT</span> `name`, stu.id, grade<br><span class="hljs-keyword">FROM</span> stu <span class="hljs-keyword">LEFT</span> <span class="hljs-keyword">JOIN</span> exam<br><span class="hljs-keyword">ON</span> stu.id <span class="hljs-operator">=</span> exam.id;<br></code></pre></td></tr></table></figure>



<h4 id="右外连接"><a href="#右外连接" class="headerlink" title="右外连接"></a>右外连接</h4><p>如果右侧的表完全显示,则是右外连接</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">--左侧表没有满足条件的数据 全部显示</span><br><span class="hljs-comment">--显示所有人的成绩，如果没有成绩，也要显示该人的姓名和 id 号,成绩显示为空</span><br><br><span class="hljs-keyword">SELECT</span> `name`, stu.id, grade<br><span class="hljs-keyword">FROM</span> exam <span class="hljs-keyword">RIGHT</span> <span class="hljs-keyword">JOIN</span> stu<br><span class="hljs-keyword">ON</span> stu.id <span class="hljs-operator">=</span> exam.id;<br></code></pre></td></tr></table></figure>





<h3 id="约束"><a href="#约束" class="headerlink" title="约束"></a>约束</h3><p>约束用来确保数据库的数据满足特定的规则</p>
<h4 id="primary-key-主键"><a href="#primary-key-主键" class="headerlink" title="primary key 主键"></a>primary key 主键</h4><p>​	用于唯一的表示表行的数据,当定义主键约束后,该列不能重复</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> t17<br>(id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY, <span class="hljs-comment">-- 表示 id 列是主键</span><br>`name` <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">32</span>),<br></code></pre></td></tr></table></figure>

<p>​	<strong>细节:</strong></p>
<ul>
<li><p>​	主键不能重复切不能为null</p>
</li>
<li><p>一张表最多只能有一个主键, 但可以是复合主键</p>
</li>
<li><p>使用 <code>DESC 表名</code> 可以看到主键的情况</p>
</li>
</ul>
<h4 id="not-null-非空"><a href="#not-null-非空" class="headerlink" title="not null  非空"></a>not null  非空</h4><p>​	表示必须为该列提供数据</p>
<h4 id="unique-唯一"><a href="#unique-唯一" class="headerlink" title="unique 唯一"></a>unique 唯一</h4><p>​	表示这个字段是不能重复的</p>
<p>​	<strong>细节:</strong></p>
<ul>
<li>​	如果没有指定<code>NOT NULL</code> 则<code>UNIQUE</code> 字段可以有多个null</li>
<li>一张表可以有多个<code>UNIQUE</code>字段</li>
</ul>
<p>​	</p>
<h4 id="foreign-key-外键"><a href="#foreign-key-外键" class="headerlink" title="foreign key 外键"></a>foreign key 外键</h4><p>​	用来定义主表和从表之间的关系,  外键主要约定在从表上,  主表则必须具有主键约束或者是<code>UNIQUE</code>约束</p>
<p>​	当定义外键约束后,要求外键列数据必须在主表的主键列存在 , 或者是为null</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">-- 创建 主表 my_class</span><br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> my_class (<br>id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY , <span class="hljs-comment">-- 班级编号</span><br>`name` <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">32</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">&#x27;&#x27;</span>); <span class="hljs-comment">-- 创建 从表 my_stu</span><br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> my_stu (<br>id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY , <span class="hljs-comment">-- 学生编号</span><br>`name` <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">32</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">&#x27;&#x27;</span>, class_id <span class="hljs-type">INT</span> , <span class="hljs-comment">-- 学生所在班级的编号</span><br><span class="hljs-comment">-- 下面指定外键关系</span><br><span class="hljs-keyword">FOREIGN</span> KEY (class_id) <span class="hljs-keyword">REFERENCES</span> my_class(id))<br></code></pre></td></tr></table></figure>



<h4 id="check"><a href="#check" class="headerlink" title="check"></a>check</h4><p>​	用于强制行数据必须满足的条件</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> t23 (<br>id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY, `name` <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">32</span>) , sex <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">6</span>) <span class="hljs-keyword">CHECK</span> (sex <span class="hljs-keyword">IN</span>(<span class="hljs-string">&#x27;man&#x27;</span>,<span class="hljs-string">&#x27;woman&#x27;</span>)), <br><span class="hljs-comment">-- 规定sal字段只能在1000~2000之间</span><br>sal <span class="hljs-keyword">DOUBLE</span> <span class="hljs-keyword">CHECK</span> ( sal <span class="hljs-operator">&gt;</span> <span class="hljs-number">1000</span> <span class="hljs-keyword">AND</span> sal <span class="hljs-operator">&lt;</span> <span class="hljs-number">2000</span>)<br>);<br></code></pre></td></tr></table></figure>



<h4 id="auto-increment-自增"><a href="#auto-increment-自增" class="headerlink" title="auto increment  自增"></a>auto increment  自增</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> t24<br>(id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY AUTO_INCREMENT,  <span class="hljs-comment">-- id自增</span><br> email <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">32</span>)<span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">&#x27;&#x27;</span>, <br> `name` <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">32</span>)<span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">&#x27;&#x27;</span>);<br></code></pre></td></tr></table></figure>





<h3 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h3><p><strong>代价:</strong></p>
<ul>
<li>对磁盘占用</li>
<li>对增删改语句的效率影响</li>
</ul>
<h4 id="索引的类型"><a href="#索引的类型" class="headerlink" title="索引的类型"></a>索引的类型</h4><ul>
<li>主键索引  主键自动的为主索引</li>
<li>唯一索引</li>
<li>普通索引</li>
<li>全文索引 (<code>FULLTEXT</code>) 适用于MyLsam</li>
</ul>
<h4 id="索引的使用"><a href="#索引的使用" class="headerlink" title="索引的使用"></a>索引的使用</h4><ul>
<li><p>添加索引</p>
<ul>
<li>&#96;&#96;&#96;sql<br>– 1. 如果某列的值，是不会重复的，则优先考虑使用 unique 索引, 否则使用普通索引<br>– 添加唯一索引<br>CREATE UNIQUE INDEX id_index ON t25 (id);<br>– 添加普通索引方式 1<br>CREATE INDEX id_index ON t25 (id);<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs pgsql"><br>    <br><br>- 查询索引<br><br>  - ```<span class="hljs-keyword">sql</span><br>    <span class="hljs-comment">-- 查询索引</span><br>    <span class="hljs-comment">-- 1. 方式</span><br>    <span class="hljs-keyword">SHOW</span> <span class="hljs-keyword">INDEX</span> <span class="hljs-keyword">FROM</span> t25<br>    <span class="hljs-comment">-- 2. 方式</span><br>    <span class="hljs-keyword">SHOW</span> INDEXES <span class="hljs-keyword">FROM</span> t25<br>    <span class="hljs-comment">-- 3. 方式</span><br>    <span class="hljs-keyword">SHOW</span> KEYS <span class="hljs-keyword">FROM</span> t25<br>    <span class="hljs-comment">-- 4 方式</span><br>    <span class="hljs-keyword">DESC</span> t25<br></code></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>删除索引</p>
<ul>
<li><pre><code class="sql">-- 删除索引
DROP INDEX id_index ON t25
-- 删除主键索引
ALTER TABLE t26 DROP PRIMARY KEY
</code></pre>
</li>
</ul>
</li>
<li><p>修改索引 ..  先删除再新增</p>
</li>
</ul>
<p><strong>创建索引注意点</strong></p>
<ul>
<li>较频繁的作为查询条件的字段应该创建索引</li>
<li>唯一性太擦汗的字段不适合单独创建索引,即使频繁作为查询条件</li>
<li>更新非常频繁的字段不适合创建索引  (更新索引的代价比较大)</li>
<li>不会出现再<code>WHERE</code> 字句中的字段不该创建索引</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Java基础学习</div>
      <div>https://deng-zixiong.gitee.io/dzx-blog/2022/12/06/Java基础学习/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年12月6日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/dzx-blog/2023/03/01/%E7%B3%BB%E7%BB%9F%E5%B0%8F%E7%9F%A5%E8%AF%86/" title="系统知识">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">系统知识</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/dzx-blog/2021/10/01/spring%20boot%20%E5%BC%80%E5%8F%91%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/" title="spring开发中遇到的问题">
                        <span class="hidden-mobile">spring开发中遇到的问题</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/dzx-blog/js/events.js" ></script>
<script  src="/dzx-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/dzx-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/dzx-blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/dzx-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
